Creating and using String variables
A string variable is essentially a byte array that is terminated by a null (represented by 0). A string is intended to hold only ASCII characters.
The syntax to create a string is : -
Dim String Name as String * String Length
String Name can be any valid variable name. See Dim . String Length can be any value up to 8192, allowing up to 8192 characters to be stored.
The line of code below will create a String named ST that can hold 20 characters: -
Dim MyString as String * 20
Two or more strings can be concatenated (linked together) by using the plus (+) operator: -
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
'
' Create three strings capable of holding 20 characters
'
Dim DestString as String * 20
Dim SourceString1 as String * 20
Dim SourceString2 as String * 20
SourceString1 = "Hello "
' Load String SourceString1 with the text Hello
' Load String SourceString2 with the text World
SourceString2 = "World"
' Add both Source Strings together. Place result into String DestString
DestString = SourceString1 + SourceString2
The String DestString now contains the text "Hello World".
The Destination String itself can be added to if it is placed as one of the variables in the addition expression. For example, the above code could be written as: -
Device = 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim DestString as String * 20
' Create a String for 20 characters
Dim SourceString as String * 20 ' Create another String for 20 characters
DestString = "Hello "
' Load String DestString with the text Hello
SourceString = "World"
' Load String SourceString with the text World
' Concatenate DestString with SourceString
DestString = DestString + SourceString
Print DestString
' Display the result which is "Hello World"
Note that Strings cannot be subtracted, multiplied or divided, and cannot be used as part of a standard expression otherwise a syntax error will be produced.
It's not only other strings that can be added to a string, the functions Cstr, Estr, Mid$, Left$, Right$, Str$, ToUpper, and ToLower can also be used as one of variables to concatenate.
A few examples of using these functions are shown below: -
Cstr Example
' Use Cstr function to place a code memory string into a RAM String variable
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim DestString as String * 20
' Create a String for 20 characters
Dim SourceString as String * 20 ' Create another String
Dim CodeStr as Code = "World",0
SourceString = "Hello "
' Load the string with characters
DestString = SourceString + Cstr CodeStr ' Concatenate the string
Print DestString
' Display the result which is "Hello World"
The above example is really only for demonstration because if a Label name is placed as one of the parameters in a string concatenation, an automatic (more efficient) Cstr operation will be carried out. Therefore the above example should be written as: -
More efficient Example of above code
' Place a code memory string into a String variable more efficiently than
' using Cstr
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim DestString as String * 20
' Create a String for 20 characters
Dim SourceString as String * 20
' Create another String
Dim CodeStr as Code = "World",0
SourceString = "Hello "
' Load the string with characters
DestString = SourceString + CodeStr ' Concatenate the string
Print DestString
' Display the result which is "Hello World"
A null terminated string of characters held in EEPROM can also be loaded or concatenated to a String variable by using the Estr function: -
Estr Example
' Use the Estr function in order to place a
' Data memory string into a String variable
' Remember to place Edata before the main code
' so it is recognised as a constant value
Device 24F08KL200
' Choose a device with on-board EEPROM
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim DestString as String * 20
' Create a String for 20 characters
Dim SourceString as String * 20
' Create another String
Data_Str Edata "World",0
' Create a string in EEPROM
SourceString = "Hello "
' Load the string with characters
DestString = SourceString + Estr Data_Str
' Concatenate the strings
Print DestString
' Display the result which is "Hello World"
Converting an integer or floating point value into a string is accomplished by using the Str$ function: -
Str$ Example
' Use the Str$ function in order to concatenate
'an integer value into a String variable
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim DestString as String * 30
' Create a String
Dim SourceString as String * 20
' Create another String
Dim MyWord as Word
' Create a Word variable
MyWord = 1234
' Load the Word variable with a value
SourceString = "Value = "
' Load the string with characters
DestString = SourceString + Str$(Dec MyWord) ' Concatenate the string
Print DestString
' Display the result which is "Value = 1234"
Left$ Example
' Copy 5 characters from the left of SourceString
' and add to a quoted character string
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20 ' Create a String
Dim DestString as String * 20
' Create another String
SourceString = "Hello World"
' Load the source string with characters
DestString = Left$(SourceString, 5) + " World"
Print DestString
' Display the result which is "Hello World"
Right$ Example
' Copy 5 characters from the right of SourceString
' and add to a quoted character string
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20 ' Create a String
Dim DestString as String * 20
' Create another String
SourceString = "Hello World"
' Load the source string with characters
DestString = "Hello " + Right$(SourceString, 5)
Print DestString
' Display the result which is "Hello World"
Mid$ Example
' Copy 5 characters from position 4 of SourceString
' and add to quoted character strings
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20
' Create a String
Dim DestString as String * 20
' Create another String
SourceString = "Hello World"
' Load the source string with characters
DestString = "Hel" + Mid$(SourceString, 4, 5) + "rld"
Print DestString
' Display the result which is "Hello World"
Converting a string into uppercase or lowercase is accomplished by the functions ToUpper and ToLower: -
ToUpper Example
' Convert the characters in SourceString to upper case
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20
' Create a String
Dim DestString as String * 20
' Create another String
SourceString = "hello world"
' Load source with lowercase characters
DestString = ToUpper(SourceString )
Print DestString
' Display the result which is "HELLO WORLD"
ToLower Example
' Convert the characters in SourceString to lower case
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20
' Create a String
Dim DestString as String * 20
' Create another String
SourceString = "HELLO WORLD"
' Load the string with uppercase characters
DestString = ToLower(SourceString )
Print DestString
' Display the result which is "hello world"
Loading a String Indirectly
If the Source String is a signed or unsigned Byte, Word, Float or an Array variable, the value contained within the variable is used as a pointer to the start of the Source String's address in RAM.
Example
' Copy SourceString into DestString using a pointer to SourceString
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20
' Create a String
Dim DestString as String * 20
' Create another String
'
' Create a Word variable to hold the address of SourceString
'
Dim StringAddr as Word
SourceString = "Hello World"
' Load the source string with characters
'
' Locate the start address of SourceString in RAM
'
StringAddr = AddressOf(SourceString)
DestString = StringAddr
' Source string into the destination string
Print DestString
' Display the result, which will be "Hello"
Slicing a String.
Each position within the string can be accessed the same as an unsigned Byte Array by using square braces: -
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20 ' Create a String
SourceString[0] = "H"
' Place letter "H" as first character
SourceString[1] = "e"
' Place the letter "e" as the second character
SourceString[2] = "l"
' Place the letter "l" as the third character
SourceString[3] = "l"
' Place the letter "l" as the fourth character
SourceString[4] = "o"
' Place the letter "o" as the fifth character
SourceString[5] = 0
' Add a null to terminate the string
Print SourceString
' Display the string, which will be "Hello"
The example above demonstrates the ability to place individual characters anywhere in the string. Of course, you wouldn't use the code above in an actual BASIC program.
A string can also be read character by character by using the same method as shown above: -
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20 ' Create a String
Dim Var1 as Byte
SourceString = "Hello"
' Load the source string with characters
' Copy character 1 from the source string and place it into Var1
Var1 = SourceString[1]
Print Var1
' Display character extracted from string. Which will be "e"
When using the above method of reading and writing to a string variable, the first character in the string is referenced at 0 onwards, just like an unsigned Byte Array.
The example below shows a more practical String slicing demonstration.
' Display a string's text by examining each character individually
Device 24FJ64GA002
' Select the device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Dim SourceString as String * 20
' Create a String
Dim Charpos as Byte
' Holds the position within the string
SourceString = "Hello World"
' Load the source string with characters
Charpos = 0
' Start at position 0 within the string
Repeat
' Create a loop
' Display the character extracted from the string
Print SourceString[Charpos]
Inc Charpos
' Move to the next position within the string
' Keep looping until the end of the string is found
Until Charpos = Len(SourceString)
Notes.
A word of caution regarding Strings: If you're familiar with interpreted BASICs and have used their String variables, you may have run into the "subscript out of range" error. This error occurs when the amount of characters placed in the string exceeds its maximum size.
For example, in the examples above, most of the strings are capable of holding 20 characters. If your program exceeds this range by trying to place 21 characters into a string only created for 20 characters, the compiler will not respond with an error message. Instead, it will access the next RAM location past the end of the String.
If you are not careful about this, it can cause all sorts of subtle anomalies as previously loaded variables are overwritten. It's up to the programmer (you!) to prevent this from happening by ensuring that the String in question is large enough to accommodate all the characters required, but not too large that it uses up too much precious RAM.
The compiler will help by giving a reminder message when appropriate, but this can be ignored if you are confident that the String is large enough.