Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 8: Crunching Numbers and Playing with Strings 105

LEN(“Greetings from Mars!”)

In this example, the length of the string is 20 characters, including spaces and the exclamation point. Run the following program to see how the LEN function works:

PROMPT “Type a string:”; CountMe$ TotalLength = LEN(CountMe$)

PRINT “The total number of characters in your string is:” PRINT TotalLength

END

So if you run this program and type in the string Beware of copycat publishers, you’ll see the following:

The total number of characters in your string is:

28

In calculating the total length of a string, the LEN function removes any spaces before or after any visible characters, as shown in Table 8-3.

Table 8-3 How the LEN Function Counts Spaces in a String

String

String Length

“ Hello!”

6 characters (eliminates the five spaces in front of

 

the string)

“What are you looking at? “ 24 characters (eliminates the five spaces at the end

 

of the string)

“Boo! Go away!”

13 characters (counts the spaces between the

 

words)

Trimming the front and back of a string

Because strings can have spaces before (known as leading spaces) or after (known as trailing spaces) any visible character, you may want to eliminate these leading or trailing spaces before counting the length of a string.

Fortunately, Liberty BASIC includes a special function to do just that, as the following example shows:

TRIM$(“ Hello, there!”) ‘ “Hello, there!”

TRIM$(“Good-bye! “) ‘ “Good-bye”

106 Part II: Learning Programming with Liberty BASIC

To see how the TRIM$ function can strip away leading and trailing spaces, try the following program:

AString$ = “ Hello, there!”

PRINT “The original length of the string = “; LEN(AString$)

TempString$ = TRIM$(AString$)

PRINT TempString$

PRINT “The new length is now = “; LEN (TempString$)

END

If you run this program, this is what you see:

The original length of the string = 14

Hello, there!

The new length is now = 13

Inserting spaces

Sometimes you may want to create spaces without pressing the spacebar multiple times to do so. For an easy way to create spaces, Liberty BASIC provides the SPACE$ function, which looks as follows:

SPACE$(X)

In this example, X represents the number of spaces that you want to insert. The SPACE$ function in the following program simply inserts five spaces between the string Hello and the string Bo the Cat.:

AString$ = “Bo the Cat.”

PRINT “Hello” + SPACE$(5) + AString$

END

Yanking characters out of a string

If you have a long string, you may want only part of that string. You may have a string consisting of somebody’s first and last name, for example, but you want only the last name. You can use one of the following Liberty BASIC functions to rip away one or more characters from a string:

LEFT$ (string, X) rips away X number of characters starting from the left of the string.

RIGHT$ (string, X) rips away X number of characters starting from the right of the string.

MID$ (string, X, Y) rips away Y number of characters, starting at the Xth character from the left.

Chapter 8: Crunching Numbers and Playing with Strings 107

To see how these three functions work, run the following program and see what happens:

FakeName$ = “John Barkley Doe”

FirstName$ = LEFT$(FakeName$, 4)

PRINT “This is the first name = “; FirstName$

LastName$ = RIGHT$(FakeName$, 3)

PRINT “This is the last name = “; LastName$

MiddleName$ = MID$(FakeName$, 6, 7)

PRINT “This is the middle name = “; MiddleName$

END

This program does nothing more than strip out the first, last, and middle names from the longer string John Barkley Doe.

Looking for a string inside another string

If you have a long string, you may want to know the location of another word or phrase inside the longer string. Suppose, for example, that you had a string containing a list of names, as in the following example:

“John, Julia, Matt, Mike, Sam, Chris, Karen”

If you want to know in what position the name “Matt” appears in the preceding string, you can use the magical INSTR command, as follows:

Names$ = “John, Julia, Matt, Mike, Sam, Chris, Karen”

Position = INSTR(Names$, “Matt”, 1)

PRINT “The name Matt is located in position = “; Position END

This Liberty BASIC program tells the computer to do the following:

1.The first line creates the string variable Names$ that stores the string

John, Julia, Matt, Mike, Sam, Chris, Karen.

2.The second line tells the computer to set the value of the Position variable to the value that the INSTR function returns.

The INSTR function tells the computer, “Starting at position 1, look at the string that the variable Names$ represents and look for the string Matt.” In this case, the string Matt appears in position 14 in the Names$ string.

3. The third line prints The name Matt is located in position = 14.

4. The fourth line tells the computer that the program is at an end.

108 Part II: Learning Programming with Liberty BASIC

To use the INSTR function, you need to specify the following three items:

The position where you want to start searching.

In the preceding example, the position is 1, which represents the start of the string. You can, however, tell the computer to start looking from any position. If you tell the computer to start looking from position 20, it can never find the name “Matt” in the Names$ variable.

The string that you want to search.

The string that you want to locate.

Strings are case-sensitive, which means that, to the computer, the strings “MATT” and “Matt” are two completely different strings. In the following program, INSTR can’t find the string “MATT” because it thinks the strings “MATT” and “Matt” aren’t the same:

Names$ = “John, Julia, Matt, Mike, Sam, Chris, Karen”

Position = INSTR(Names$, “MATT”, 1)

PRINT “The name Matt is located in position = “; Position

END

If the INSTR function can’t find the string that you’re looking for, the value of the INSTR function is zero.

Converting strings into numbers

(and vice versa)

A string can consist of letters, symbols, and numbers. The most common use for storing numbers as a string is if the numbers represent something special, such as a telephone number or address. If you want the computer to print a telephone number, you must store that number as a string, as follows:

PRINT “555-1212”

This command prints the string 555-1212 on-screen. The quotation marks tell the computer, “Anything inside the quotation marks is a string.” What happens if you try the following command?

PRINT 555-1212

Liberty BASIC interprets this command to mean, “Subtract the number 1,212 from the number 555 and print the result (which is –657) on-screen.”

If you have a number but you want to treat it as a string, you can use Liberty BASIC’s STR$ function, which works as follows:

Chapter 8: Crunching Numbers and Playing with Strings 109

STR$(Number)

The STR$ function tells Liberty BASIC, “Take a number (such as 45) and turn it into a string.” If you have a number 34 and use the STR$ function, Liberty BASIC converts that number 34 into a string “34”.

To print both a string and a number, for example, you normally need to use the PRINT command with a semicolon, as in the following example:

BossIQ = 12

PRINT “This is the IQ of your boss =”; BossIQ

END

You can, however, use the STR$ function to convert the BossIQ variable into a string, as follows:

BossIQ = 12

NewString$ = “This is the IQ of your boss = “ + STR$(BossIQ)

PRINT NewString$

END

The main difference between the first example and the second example is that the number BossIQ is now part of the string NewString$.

Naturally, Liberty BASIC enables you to convert strings into numbers as well. If you have a string (such as “46”), you can’t perform any mathematical operations on it. You can, however, convert that string into a number by using Liberty BASIC’s VAL function, which works as follows:

VAL(“String”)

The VAL function tells Liberty BASIC, “Take a string (such as “45”) and turn it into a number.” If you have a string “45” and use the VAL function, Liberty BASIC converts that string “45” into the number 45.

If you use the VAL function on a string that doesn’t represent a number, such as the string “Hello!”, the VAL function returns a zero value.

To see how the VAL function works, run the following program:

YearBorn$ = “1964”

PROMPT “You were born in “; YearBorn$

Year = 2005 – VAL(YearBorn$)

NOTICE “In 2005, you were this old = “; Year

END

If you run this program, the program displays a Prompt dialog box that says, You were born in 1964. Then it displays a Notice dialog box that says, In 2005, you were this old = 35.

110 Part II: Learning Programming with Liberty BASIC