Strings variables are variables that hold text.
A string variable's name always ends in $. (NAME$, S$, etc.)
Strings in SB are 0-indexed, which means that the "first" character is actually the 0th.
"ABC"[0] is A
String functions:
Code% =
ASC(String$)
-returns the ASCII value of the first character in the string.
-gives an error if used on an empty string.
?ASC("ABC")
65
String$ =
CHR$(Code%)
-the inverse function of ASC, converts an ASCII value to a 1 character-long string.
?CHR$(65)
A
Number =
VAL(String$)
-gets the number contained in a string.
-if the string is not a valid number, returns 0
?VAL("12.3")
12.3
String$ =
STR$(Number {,
Digits%} )
-converts
Number into a string. Inverse of VAL. If
Digits% is specified, it will make the string that long, with spaces on the left.
?STR$(157,4)
157
Hexadecimal$ =
HEX$(
Number% {,
Numberofdigits%} )
-converts a decimal
Number into hexadecimal. Since hexidecimal has more than 10 numerals, it must be stored as a string. If
Digits is specified, leading zeroes will be added.
?HEX$(15,3)
00F
String$ =
FORMAT$(
format$, List {,...} )
-similar to
printf in other languages
-explanation coming soon (this one is pretty complicated!)
Length% =
LEN(
String$)
-returns the length of
String$
?LEN("ABC")
3
SubString$ =
MID$(
String$,
Start%,
Length%)
-returns
Length% characters, beginning at
Start%, from
String$
?MID$("ABCD",2,2)
BC
-MID$(String$,Position%,1) can be replaced by String$[Position%]
SubString$ =
LEFT$(
String$,
Length%)
-returns the
Length% leftmost characters from
String$
?LEFT$("ABC",2)
AB
SubString$ =
RIGHT$(
string$,
Length%)
-returns the
Length% rightmost characters from
String$
?RIGHT$("ABC",2)
BC
Position =
INSTR({
Start%,}
String$,
Search$)
-returns the first position of
Search$ in
String$, starting from
Start% (if specified). Returns -1 if the character is not found.
?INSTR("HELLO","L")
2
?INSTR(3,"HELLO,"L")
3
?INSTR("HELLO","D")
-1
SubString$ =
SUBST$(
String$,
Start% {,
Length%},
Replacement$)
-returns
String$ with the first
Length% characters starting from
Start% replaced by
Replacement$. If
Length% is not specified, it will replace everything after
Start%
?SUBST$("A?D",1,1,"BC")
ABCD