4 Data Variables and Data handling
Each string variable name is terminated with a dollar sign ($). For example:
X$ Home_address$ Part_no1$Characters can be assigned to a string (or substring) variable using the LET statement:
[LET] string (or substring) variable [=string variable ... ]=string expression
For example:
LET Title$="Chapter 1" Asterisks$="*********" Married_name$=Husbands_name$="Smith"There are many other ways to assign values, as shown later in this chapter.
For example, the following statement prints the string contained in the quoted element of the three-dimensional string array Name_array$:
PRINT Name_array$(1,3,4)
Substrings and string concatenation are covered in this chapter. The next chapter covers the built-in string functions and shows how to define your own string functions.
There are three ways to designate a substring. The first method is to indicate the starting index (position) of the substring, followed by a comma and the ending index of the substring. All indexes are inclusive. For example, if Example_string$ = "ABCDEFGHIJ", then:
Example string$[1,3] = "ABC" Example_string$[4,6] = "DEF" Example_string$[7,10] = "GHIJ" Example_string$[1,10] = "ABCDEFGHIJ"Notice that Example_string$[1,10] is effectively the same as Example_string$.
The second method of designating a substring is to give the position of the first character, followed by a semicolon and the length of the substring. Continuing the above example:
Example_string$[1;3] = "ABC" Example_string$[4;3] = "DEF" Example_string$[7;4] = "GHIJ" Example_string$[1;10] = "ABCDEFGHIJ"Here too, Example_string$[1;10] is effectively the same as Example_string$.
The third method of designating a substring is to give the starting index of the substring only. This is really a special case of the first method, in which the ending index is assumed to be the length of the string. Continuing the example:
Example_string$[1] = "ABCDEFGHIJ" Example_string$[5] = "EFGHIJ" Example_string$[10] = "J"So Example_string$[1] is effectively the same as Example_string$.
Regardless of which method is used, the first position indicator, the second position indicator, and the length indicator may be any valid numeric expression.
NOTE: If the numeric expression is not a whole number, it will be rounded to the nearest integer.
Here are more examples:
Example_string$[N,M] Example_string$[F;L] Example_string$[N + SQR(Z);L-1] ! so long as Z > 0! Example_string$[A(I),A(J)]
A$=Example_string$[1;0] ! Set A$ to the NULL string Test$="A null"&A$&"string" !There will be no space between null and string A nullstringThere is a special case, however, with a null substring. When a null substring is assigned to a string then the characters covered by that substring become blanks. The string length is not altered. For example, using our previous test:
Example_string$="ABCDEFGHIJ" Example_string$[3;2]=""!Assign a null string to characters 3 and 4 AB EFGHIJCharacters 3 and 4 are changed to blanks and Example_string$ is still 10 characters long.
All strings are initialized to the null string by executing RUN, SCRATCH C, or SCRATCH V.
20 The$="the" 30 Example$="I am "&The$&" "&CHR$(38)&" sign." 40 PRINT Example$ I am the & sign
10 DIM Faculty_title$[22]The string Faculty_title$ has maximum length of 22 characters.
20 Faculty_title$="School of " Initially, Factualty_title is a 30 LET B$="Business" 10 character string "School of " 50 En$="Engineering" ( indicates a blank space) 60 L$="Law" 70 Bw$="Basket Weaving" 80 C$="Civil" 100 Faculty_title$[11]=B$The string B$ is inserted into the string Faculty_title$ beginning at the 11th character of Faculty_title$.
130 PRINT Faculty_title$ Prints: School of Business 170 Faculty_title$[11]=En$ 210 PRINT Faculty_title$ Prints: School of BusinessIf single subscripts are used when assigning substrings (as in the examples above) and the item being assigned is too short to fill the string, then the rest of the receiving string is truncated. Thus, continuing the example, when the string L$ is inserted at character 11 in Faculty_title$, the rest of Faculty_title$ is erased:
250 Faculty_title$[11]=L$ 290 PRINT Faculty_title$ Prints: School of LawIf single subscripts are used and the item being assigned is too long for the substring, an error is returned:
420 Faculty_title$[11]=Bw$ ERROR 18 IN LINE 420An attempt to insert the 14 chars of Bw$ in the 11 remaining chars of Faculty_title$ returns ERROR 18.
If two subscripts are used when assigning substrings, then the changes will only be made in the range of the receiving string specified by the subscripts. The length of the receiving string is retained and any receiving string characters outside the subscript range will be unaffected. To continue the example:
450 Faculty_title$[11,22]=B$L$ 460 PRINT Faculty_title$ Prints: School of BusinessLawNote that the concatenation operator (&) does not insert a blank. A better title would be printed by:
500 Faculty_title$[11,22]=B$&" "L$ 510 PRINT Faculty_title$ Prints: School of Business LawNow for the Civil Law faculty. Note that if the substring to be added is too short, the rest of the receiving string, within the range specified by the two subscripts, is filled with blanks:
450 Faculty_title$[11,18]=C$ 460 PRINT Faculty_title$ Prints: School of Civil LawIf the substring to be added is too long, it is truncated to fit in the length specified by the subscripts--no error is indicated.
590 Faculty_title$[11;12]=Bw$ 600 PRINT Faculty_title$ Prints: School of Basket WeaviHere is a summary of substring errors and their error codes:
Invalid Designator | Action |
---|---|
Starting index < 1 | Error 18 |
Ending index < 0 | Error 18 |
Starting index > ending index + 1 | Error 18 |
Starting index = ending index + 1 | Null string |
String length = 0 | Null string |
Starting index > current length of string + 1 | Error 18 |
Ending index > dimensioned length of string | Error 18 |