String variables allow you to manipulate
text data. You can define a string variables by using the global
command.
global myvarb(20) as string
This defines a 20 byte string variable.
You can store up to 19 characters in this variable. The last position
is reserved for use as a string terminator. There is only 256 bytes of
string memory available so use it wisely. The use of the table command
can aid you in cutting down string space usage.
Once a variable has been defined it can
be assigned values.
Direct Assignment
myvarb = "jump"
myvarb will contain
jump
myvarb2 = myvarb+" down"
myvarb2 will contain
jump down
myvarb = * + " up"
myvarb will contain
jump up
String Insertion
myvarb="ABCDEFGH"
myvarb(3)="mike"
myvarb will contain
ABCmikeH
myvarb(2)=90
myvarb will contain
ABZmikeH
myvarb(5)="1234567"
myvarb will contain
ABZmi1234567
Partial string Assignment
myvarb = "ABCDEFG"
myvarb2 = myvarb(1,5) 'Starting character
1 for 5 characters
myvarb2 will contain
BCDEF
myvarb2 = myvarb(1,200) 'Starting
character 1 for 200 characters or end of string
myvarb2 will contain
BCDEFG
There are some restriction in using
string variables usage.
You can not assign a string variable to
its self. For instance myvarb = myvarb +"s" is not allowed. If you
need to add something to the end of an existing string use the *
operator. This tells the string pointer to move to the end of the
string.
You can not pass a variable to a
function or return a string from a function. You can however pass a
reference to the string so that the function can compare or manipulate
it.
The math operators do not work the same
when using strings. For instance.
myvarb=65
myvarb will contain
A
myvarb=90+89+87
myvarb will contain
ZYX
Assigning a integer variable to a string
works the same way. The lower 8 bits are converted to ASCII and added
to the string.
There is no bounds checking on string
assignments. So if you assign something out side of a string defined
size it will be inserted into the next string. This could allow you to
index many strings with a bit of math.
Once a string is defined its contents
are unknown until you assign something to it.
String Terminator
An end of a string indicated by a 0
value character. Hence a string "jump" will actually contain
106,117,109,112,0
Most of the manipulation of the string
terminator is done automatically. You can also manipulate the
terminator.
global myvarb(20) as string
myvarb=0
or
myvarb(0)
will create an empty string
myvarb="ABCDEFG"
myvarb(3)=0
myvarb will contain
ABC
Notes on string insertion
As long as the insertion is located
inside the bounds of the original string no terminator will be
inserted.
globalvarb1(10) as string
varb1="ABCDEFGH"
varb1(3)=90
varb1 will contain
ABCZEFGH
If the insertion point starts inside the
bounds and extends outside the bounds a new terminator will be added
to adjust the length.
globalvarb1(20) as string
varb1="ABCDEFGH"
varb1(5)="1234567"
varb1 will contain
ABCDE1234567
If the insertion point is outside the
original string no terminator will be inserted.
global varb1(10) as string
globalvarb2(10) as string
varb1="ABCDEFGH"
varb2="123456789"
varb1(13) = "mike"
varb1 will contain
ABCDEFGH
varb2 will contain
123mike89
String Address Operator
You can use the string address operator
to assign a string based on and address. Lets take the following
example.
global varb1(20) as string
global varb2(20) as string
note that the address of varb2 is 20 (end
of varb1)
varb2="Mike"
varb1=@20
The address operator takes the integer
expression value and treats it like a string address. This can be
useful in quick string or table access with functions where the
address is passed.