www.riscos.com Technical Support:
String variables may be used to store strings of characters, constituting words and phrases. This chapter shows you how to assign values to a string variable, and describes several useful operations you can perform on strings in BASIC; such as splitting a string and joining two or more strings together.
Each string can be up to 255 characters long. The following gives some examples of strings:
day$ = "Monday" Date$ = "29th February" space$ = " " Address$ = "10 Downing Street, London" Age$ = "21"
Note that the variable Age$ is assigned a string containing the two characters 2 and 1, and not the number 21. So, if you type
Real_Age$ = 21 * 2
the result will not be "42" because BASIC cannot do arithmetic with strings. Instead, the error message:
Type mismatch: string needed
appears on the screen, indicating that only a string expression can be assigned to a string variable. A type mismatch error can also be caused by an attempt to multiply strings, as in:
total$ = "12"*"32"
You should note that the 'null' string "" is valid. This is a string containing zero characters. In comparisons, it is less than any other string (except, of course, another null string).
In order to obtain a double quotation character, ", in a string, you use two of them adjacent to each other. For example, to print the text A"here, you would use:
Two strings may be joined together, or, more correctly speaking, concatenated. The + operator is used to indicate this:
10 Road$ = "Downing Street" 20 City$ = "London" 30 PRINT Road$ + " " + City$
Typing RUN produces the following:
Downing Street London
The += operator can also be used, and as the following program shows, produces the same output as +.
10 Address$ = "Downing Street" 20 Address$ += " " 30 Address$ += "London" 40 PRINT Address$
Note, however, that the -= operator is meaningless when applied to strings and produces an error message.
As well as joining two strings together, BASIC can split a string into smaller sequences of characters. Three functions are provided for doing this.
HE RE RD
10 title$ = "Moonlight Sonata" 20 left_of_string$ = LEFT$(title$,4) 30 right_of_string$ = RIGHT$(title$,6) 40 middle_of_string$ = MID$(title$,5,9) 50 PRINT left_of_string$ 60 PRINT right_of_string$ 70 PRINT middle_of_string$
produces the following when run:
Moon Sonata light Son
Each of these functions has a convenient shorthand form:
10 PRINT LEFT$("Hello") 20 PRINT RIGHT$("Hello") 30 PRINT MID$("Hello",3)
produces the following:
Hell o llo
LEFT$, RIGHT$ and MID$ may be used to replace part of a string. In each case the number of new characters equals the number of characters being replaced, and the string stays the same length. The number of characters being changed can be determined by the length of the replacement string. Thus:
10 A$ = "Hello there." 20 MID$(A$,7) = "Susan" 30 PRINT A$ 40 LEFT$(A$) = "Howdy" 50 PRINT A$ 60 RIGHT$(A$) = "!" 70 PRINT A$
Hello Susan. Howdy Susan. Howdy Susan!
Alternatively, you can give the maximum number of characters to be replaced. Then, if the length of the replacement string is less than the given value, all of it is used. Otherwise only the first designated number of characters have an effect. For example,
10 A$ = "ABCDEFGHIJ" 20 RIGHT$(A$,3) = "KL" 30 PRINT A$ 40 LEFT$(A$,4) = "MNOPQR" 50 PRINT A$ 60 MID$(A$,4,3) = "STUVW" 70 PRINT A$
ABCDEFGHKL MNOPEFGHKL MNOSTUGHKL
There are also BASIC keywords to:
These keywords are:
produces the output:
The statement PRINT LEN("PAUL") prints the number 4 and
A$ = "Great Britain" PRINT LEN(A$)
produces the result 13. Note that the space is treated like any other character.
A$ = "Great Britain" PRINT INSTR(A$,"it")
prints 9 because the string it is contained in Great Britain at the ninth character position. If the substring in the INSTR function is not present in the first string, then 0 is returned. Note also that you can start the search for the substring at any position, not just from the start of the substring. This is done by specifying a third parameter, so that for example,
PRINT INSTR("'ello 'ello","'ello",2)
will print 7, since the first occurrence of the substring will be skipped.
You can use the relational operators >, =, <= etc, to compare two strings. See the chapter entitled Control statements for details.
Every character and symbol which can be reproduced on the screen is represented within the computer by a number in the range 0 to 255. The system used to assign numbers to characters and symbols is known as ISO-8859. This is an extension of the very popular ASCII (American Standard Code for Information Interchange) code, which only applies to characters between 0 and 127. We shall use ASCII as a general term for character codes. It is wise to follow such a standard so that different computers can all understand the same numerical alphabet.
BASIC provides a pair of functions for converting characters to their ASCII number-codes and back again. These are:
There are three keywords which convert between strings and numbers:
VAL returns the value of a string, up to the first non-numeric character.
prints the value 10, since all the characters after the t are ignored. The string may, however, begin with a + or -. Thus,
number = VAL("-5")
assigns the value -5 to number. If, however, the string does not start with a digit or a plus or minus sign, VAL returns 0.
EVAL however, considers the whole string as an expression, allowing operators and variable names to occur within it. Variables must be assigned values beforehand.
10 radius = 5 20 area = EVAL("PI*radius^2") 30 PRINT area
When this program is run the value printed is 78.5398163, which is the value PI (3.141592653) multiplied by 5 squared.
STR$ performs the opposite conversion to the above two functions. It takes the number given and returns a string containing the digits in the number.
10 A = 45 20 B = 30.5 30 A$ = STR$(A) 40 B$ = STR$(B) 50 PRINT A + B 60 PRINT A$ + B$
produces the following when it is run:
BBC BASIC can express numbers in base 16 (hexadecimal) as well as base 10 (decimal). This is useful for dealing with certain types of integer. The chapter entitled Bases explains more about the various ways in which bases can be used. STR$~x gives the hexadecimal string representation of x. Thus
10 A = 45 20 A$ = STR$~(A) 30 PRINT A$
because 2D is the hexadecimal version of the decimal number 45.