String Comparisons
Just like any other variable type, String variables can be used within comparisons such as If- Then, Repeat-Until, and While-Wend . In fact, it's an essential element of any programming language. However, there are a few rules to obey because of the PICmicro's architecture.
Equal (=) or Not Equal (<>) comparisons are the only type that apply to Strings, because one String can only ever be equal or not equal to another String. It would be unusual (unless your using the C language) to compare if one String was greater or less than another.
So a valid comparison could look something like the lines of code below: -
If String1 = String2 Then Print "Equal" : Else : Print "Not Equal"
or
If String1 <> String2 Then Print "Not Equal" : Else : Print "Equal"
But as you've found out if you read the Creating Strings section, there is more than one type of String in a Positron8. There is a RAM String variable, a flash memory string, and a quoted character string .
Note that pointers to String variables are not allowed in comparisons, and a syntax error will be produced if attempted.
Starting with the simplest of string comparisons, where one string variable is compared to another string variable. The line of code would look similar to either of the two lines above.
Example 1
' Simple string variable comparison
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud Rate for HRsoutLn
' Create Strings capable of holding 20 characters
Dim String1 as String * 20 = "EGGS" ' Pre-load String String1 with the text EGGS
Dim String2 as String * 20 = "BACON" ' Load String String2 with the text BACON
If String1 = String2 Then ' Is String1 equal to String2?
HRsoutLn " Equal " ' Yes. So display Equal
Else ' Otherwise
HRsoutLn "Not Equal " ' Display Not Equal
EndIf
String2 = "EGGS" ' Now make the strings the same as each other
If String1 = String2 Then ' Is String1 equal to String2?
HRsoutLn "Equal" ' Yes. So display Equal
Else ' Otherwise
HRsoutLn "Not Equal " ' Display Not Equal
EndIf
Stop
The example above will display "Not Equal" on the serial terminal because String1 contains the text "EGGS" while String2 contains the text "BACON", so they are clearly not equal.
The line below on the serial terminal will show "Equal" because String2 is then loaded with the text "EGGS" which is the same as String1, therefore the comparison is equal.
A similar example to the previous one uses a quoted character string instead of one of the String variables.
Example 2
' String variable to Quoted character string comparison
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud Rate for HRsoutLn
Dim String1 as String = "EGGS" ' Pre-load String String1 with the text EGGS
If String1 = "BACON" Then ' Is String1 equal to "BACON"?
HRsoutLn "Equal" ' Yes. So display equal
Else ' Otherwise…
HRsoutLn "Not Equal" ' Display Not Equal
EndIf
If String1 = "EGGS" Then ' Is String1 equal to "EGGS"?
HRsoutLn "Equal" ' Yes. So display Equal
Else ' Otherwise…
HRsoutLn "Not Equal" ' Display Not Equal
EndIf
Stop
The example above produces exactly the same results as example1 because the first comparison is clearly not equal, while the second comparison is equal.
Example 3
' Use a string comparison in a Repeat-Until loop
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
Dim SourceString as String * 20 = "Hello" ' Load String SourceString with Hello
Dim DestString as String * 20 ' Create another String
Dim bCharpos as Byte ' Character position within the strings
Clear DestString ' Fill DestString with nulls
Repeat ' Create a loop
'
' Copy SourceString into DestString one character at a time
'
DestString[Charpos] = SourceString[bCharpos]
Inc bCharpos ' Move to the next character in the strings
Until DestString = "Hello" ' Stop when DestString is equal to the text "Hello"
HRsoutLn DestString ' Display DestString
Example 4
' Compare a string variable to a string held in flash memory
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim String1 as String * 20 = "BACON" ' Pre-load String String1 with text BACON
Dim FlashString as Flash = "EGGS", 0 ' Create a flash memory character string
If FlashString = "BACON" Then ' Is FlashString equal to "BACON"?
HRsoutLn " Equal" ' Yes. So display Equal
Else ' Otherwise…
HRsoutLn "Not Equal" ' Display Not Equal
EndIf
String1 = "EGGS" ' Pre-load String String1 with the text EGGS
If String1 = FlashString Then ' Is String1 equal to FlashString?
HRsoutLn " Equal" ' Yes. So display Equal
Else ' Otherwise…
HRsoutLn "Not Equal" ' Display Not Equal
EndIf
Example 5
' String comparisons using Select-Case
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
Dim String1 as String * 20 = "EGGS" ' Pre-load String String1 with the text EGGS
Select String1 ' Start comparing the string
Case "EGGS" ' Is String1 equal to EGGS?
HRsoutLn "Found EGGS" ' Yes. So display it
Case "BACON" ' Is String1 equal to BACON?
HRsoutLn "Found BACON" ' Yes. So display it
Case "COFFEE" ' Is String1 equal to COFFEE?
HRsoutLn "Found COFFEE" ' Yes. So display it
Case Else ' Default to...
HRsoutLn "No Match" ' Displaying no match
EndSelect
See also : Creating and using Strings Creating and using Virtual Strings with Cdata Cdata, If-Then-Else-EndIf, Repeat-Until Select-Case, While-Wend, Do-Loop.


