The Beginners Page
Tom R. Halfhill, Editor
Cutting Strings Without Scissors
Now that we've covered the fundamentals of creating string variables over the past few columns, we can start exploring some of the more powerful string manipulations available in BASIC. Practically all BASIC languages have commands and functions for slicing strings of characters into smaller pieces, pasting two or more strings together to make longer strings, extracting certain sections from within strings, and inserting or replacing portions of strings. Some BASICs even have commands for rapidly searching through strings to find certain sequences of characters.
Since it may not be apparent why you'd want to do any of these things in your own programs, we'll show some common examples for each technique as we go along. In general, these functions give your programs the power to manipulate strings of characters for sorting, screen formatting, printing, storing and retrieving information, and other text-oriented operations.
Slicing Up Strings
Microsoft-style BASICs—such as those included with Commodore, Apple, IBM, Atari ST, and Amiga computers—generally have three functions for extracting shorter strings from longer strings: LEFTS, RIGHTS, and MID$ (pronounced "left-string," "right-string," and "mid-string"). TI BASIC has only one string function, SEG$, which is very similar to MID$. Atari BASIC, found on the 400/800, XL, and XE computers, handles string manipulations quite differently, as we'll see next month.
LEFTS and RIGHTS are easy to visualize: They extract characters from the leftmost and rightmost sections of a character string, respectively. You simply follow the keyword with the string variable you're extracting from and the number of characters you want to extract. For example:
10 A$="GEORGE WASHINGTON CARVER" 20 PRINT A$ 30 B$ = LEFT$(A$,6) 40 PRINT B$ 50 B$=RIGHT$<A$,6) 60 PRINT B$ 70 PRINT A$
When you type RUN, you should see this on the screen:
GEORGE WASHINGTON CARVER GEORGE CARVER GEORGE WASHINGTON CARVER
To see how LEFTS works, look at the statement B$=LEFT$(A$,6) in line 30. It grabs the leftmost six characters of AS—GEORGE—and stores them in B$. Line 40 confirms this by printing B$. To extract the phrase GEORGE WASHINGTON from A$, we could change line 30 to read B$ = LEFT$(A$, 17)—keeping in mind that spaces count as characters, just like letters, numbers, and symbols. (Of course, you can use your own variable names for A$ and B$ as long as you stick to this basic format.)
RIGHTS is the opposite of LEFTS: It extracts the rightmost number of characters in A$ that you specify in the RIGHTS statement. If you change line 50 to read B$ = RIGHT$(A$,17), the result is WASHINGTON CARVER.
Line 70 shows that A$ remains intact after sections of it have been extracted with the LEFTS and RIGHTS functions. LEFTS and RIGHTS actually copy sections of the string into B$, rather than cutting the sections out.
Putting Lefty To Work
If you specify a value in a LEFTS or RIGHTS statement that is greater than the length of the string—in this case, say, B$ = LEFT$(A$,35)— most BASICs return all of A$ in B$, the equivalent of B$=A$. This can happen in a program when you're unsure about the current length of A$, or if you're using a variable for the number parameter in a LEFTS or RIGHTS statement and the variable somehow is increased beyond the length of A$. If you specify a zero for this number—as in B$ = RIGHT$(A$,0)—most BASICs return a null (empty) string.
If the number you specify in the LEFTS or RIGHTS statement is greater than 255, you'll probably get an error. Most Microsoft BASICs don't allow strings longer than 255 characters, so any reference to numbers greater than 255 in string-manipulation statements is invalid. Exceptions are the latest and most advanced Microsoft BASICs, such as Macintosh Microsoft BASIC and Amiga BASIC. They allow strings up to 32,767 characters long.
Of the two functions, LEFTS is probably used more often than RIGHTS. One practical application of LEFTS is to truncate user input to a predetermined length. For instance, let's say you're writing a program that asks for the user's name. At some point your program prints the name on the screen, but you want to limit the name to ten characters to keep from messing up your screen formatting. The solution is a line such as INPUT MYNAME$:MYNAME$ = LEFT$(MYNAME$,10). Note that in this case, the original content of MYNAMES is lost, because the LEFTS function stores the leftmost ten characters back into MYNAMES.
Here's another application for LEFTS: Suppose your program asks the user a yes or no question. You can evaluate the answer with a line such as INPUT ANSWER$:IF LEFTS (ANSWER$,1)="Y" THEN GOTO 1000 (assuming that line 1000 is the beginning of your "Yes" routine). That way, your program responds correctly whether the user types Y, YES, YEAH, YEP, YES SIR, or even YOU BET.