The Beginners Page
Tom R. Halfhill, Editor
Advanced String Features
To wrap up our long-running series on character strings in BASIC, let's take a look at some advanced string features which are finding their way into the latest and most sophisticated versions of the BASIC language. Although these features may not be found in the BASIC you're working with now, you'll probably encounter them sometime in the future.
If you want to keep up with these trends, pay attention to any new version of BASIC released by Microsoft, Inc. Microsoft certainly isn't the only company in the BASIC business, but it definitely is the market leader. Versions of Microsoft BASIC are either standard equipment or available as an option for almost every microcomputer ever made. When an advanced feature is introduced in a new version of Microsoft BASIC, it tends to cross over into the next version which is released, even if the next version is for a completely different computer.
For instance, the latest Microsoft BASIC to appear is Amiga BASIC. It shares numerous features with its nearest predecessor, Microsoft BASIC for the Macintosh. These two dialects are so much alike that some example programs in the Macintosh BASIC manual—even those with graphics—will run unchanged on the Amiga.
One new trend in Microsoft BASIC is to remove the 255-character limit on strings. Macintosh BASIC and Amiga BASIC both let you define strings up to 32,767 characters long.
So what? you might say. Who needs to display a message that's thousands of characters long? It probably won't fit on the screen, anyway.
But strings are good for lots of things besides displaying messages, of course, especially if they aren't limited to 255 characters. Programmers on the Atari 400/800/XL/XE computers know this well, because Atari BASIC has allowed super strings since 1979.
For instance, suppose you want to write a simple terminal program for downloading public domain software from information services and bulletin boards. Unless you're handy with a memory map, you might have trouble finding a large area of free memory in your computer to temporarily hold the downloaded data before storing it on disk. With super strings, it's no problem. Simply download everything into a single string, perhaps called BUFFER$. Since BASIC reserves and protects memory for the string, you don't have to worry about memory conflicts.Best of all, the new Microsoft BASICs don't force you to give up anything in return for super strings—unlike Atari BASIC, which doesn't allow string arrays as a tradeoff for this feature.
Search And Replace
Another powerful feature of late-model BASICs is the INSTR function (pronounced in-string). INSTR searches through a longer string in search of any shorter string you specify. If INSTR finds the shorter string (substring), it returns a number indicating the substring's starting character position within the longer string. Example:
10 MAIN$ = "This is the longer string."
20 X = INSTR (MAIN$,"the")
When you run this program, INSTR returns the value 9 in the variable X, because the substring the begins at the ninth character position within MAIN$. Of course, you could also use a string variable for the substring parameter in the INSTR function. If INSTR can't find the substring, it returns a 0.
By adding another parameter, INSTR can be made to begin its search at any point within MAIN$.
For example, X=INSTR(5,MAIN$,SUB$) would begin searching for SUB$ at the fifth character position of MAIN$. The INSTR function makes it a snap to write filing programs with rapid search-and-retrieve features, because it works at nearly machine language speed.
Some recent BASICs (including Macintosh BASIC, Amiga BASIC, and BASIC 7.0 on the Commodore 128) allow the use of MID$ as a statement as well as a function. You'll recall from the April column that the MID$ function lets you copy a substring from within a larger string. When used as a statement, MID$ lets you replace a specified substring with another string. And the replacement string isn't limited to the length of the substring it's replacing. When coupled with INSTR, the MID$ statement makes it easy to add a search-and-replace feature to a filing program.
Finally, another useful string command found in newer BASICs is UCASE$. This converts any string of lowercase characters to uppercase. Example: PRINT UCASE$("capitalized") results in CAPITALIZED. A logical application for the UCASE$ command is to make an INSTR search routine insensitive to case. For instance, the statement X=INSTR(UCASE$(MAIN$),UCASE$(SUB$)) will make certain that INSTR will find any matching SUB$ within MAIN$, even if some of the characters are mixed uppercase and lowercase.
Watch for more features like these to keep appearing in new versions of BASIC. Although it's over 20 years old, BASIC is only now experiencing its greatest growth spurt as programmers continue demanding more and more power from this popular language.