Positron Compiler Documentation

String Comparisons

Positron16 · Revision 1.1.3.5 · PDF pages 68–70

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.

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
  HrsoutLn "Equal"
Else
  HrsoutLn "Not Equal"
EndIf

But as you've found out if you read the Creating Strings section, there is more than one type of String in a PIC24® and dsPIC33® microcontroller. There is a RAM String variable, a code 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.

' Simple string variable comparison
  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 16        ' Tell the compiler the device will be running at 16MHz

  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyString1 as String * 20   ' Create a String of 20 characters
  Dim MyString2 as String * 20   ' Create another String

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyString1 = "EGGS"             ' Pre-load String MyString1 with the text EGGS
  MyString2 = "BACON"             ' Load String MyString2 with the text BACON

  If MyString1 = MyString2 Then  ' Is MyString1 equal to MyString2?
    HrsoutLn "Equal"             ' Yes. So display Equal on a serial terminal
  Else                           ' Otherwise
    HrsoutLn "Not Equal"        ' Display Not Equal on a serial terminal
  EndIf

  MyString2 = "EGGS"             ' Now make the strings the same as each other
  If MyString1 = MyString2 Then  ' Is MyString1 equal to MyString2?
    HrsoutLn "Equal"             ' Yes. So display Equal on a serial terminal
  Else                           ' Otherwise
    HrsoutLn "Not Equal"        ' Display Not Equal on a serial terminal
  EndIf

The example above will display "Not Equal" on the serial terminal because MyString1 contains the text "EGGS" while MyString2 contains the text "BACON", so they are clearly not equal.

View original PDF page 68Open page in PDFOriginal PDF page 68

The line underneath will show "Equal" because MyString2 is then loaded with the text "EGGS" which is the same as MyString1, therefore the comparison is equal.

A similar example to the previous one uses a quoted character string instead of one of the String variables.

' String variable to Quoted character string comparison

  Device = 24FJ64GA002             ' Select the device to compile for
  Declare Xtal = 16        ' Tell the compiler the device will be running at 16MHz

  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyString as String * 20      ' Create a String of 20 characters

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyString = "EGGS"               ' Pre-load String MyString1 with the text EGGS

  If MyString = "BACON" Then        ' Is MyString equal to "BACON"?
    HrsoutLn "Equal"               ' Yes. So display Equal on a serial terminal
  Else                              ' Otherwise…
    HrsoutLn "Not Equal"           ' Display Not Equal on a serial terminal
  EndIf

  If MyString = "EGGS" Then        ' Is MyString equal to "EGGS"?
    HrsoutLn "Equal"               ' Yes. So display Equal on a serial terminal
  Else                              ' Otherwise…
    HrsoutLn "Not Equal"           ' Display Not Equal on a serial terminal
  EndIf

The example above produces exactly the same results as example1 because the first comparison is clearly not equal, while the second comparison is equal.

' Use a string comparison in a Repeat-Until loop
  Device = 24FJ64GA002             ' Select the device to compile for
  Declare Xtal = 16        ' Tell the compiler the device will be running at 16MHz

  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim SourceString as String * 20  ' Create a String
  Dim DestString as String * 20   ' Create another String
  Dim Charpos as Byte               ' Character position within the strings

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX
  Clear DestString                  ' Fill DestString with nulls
  SourceString = "Hello"           ' Load String SourceString with the text Hello
  Repeat                           ' Create a loop
     '
     ' Copy SourceString into DestString one character at a time
     '
     DestString[Charpos] = SourceString[Charpos]
    Inc Charpos                    ' Move to the next character in the strings
     '
     ' Stop when DestString is equal to the text "Hello"
     '
  Until DestString = "Hello"
  HrsoutLn DestString               ' Display DestString on a serial terminal
View original PDF page 69Open page in PDFOriginal PDF page 69
' Compare a string variable to a string held in flash memory
  Device = 24FJ64GA002             ' Select the device to compile for
  Declare Xtal = 16        ' Tell the compiler the device will be running at 16MHz
  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyString as String * 20      ' Create a String of 20 characters
  Dim CodeString as Code = "EGGS", 0 ' Create a flash memory string

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyString = "BACON"               ' Pre-load String MyString with the text BACON
  If CodeString= "BACON" Then      ' Is CodeString equal to "BACON" ?
    HrsoutLn " Equal "             ' Yes. So display equal on a serial terminal D
  Else                              ' Otherwise…
    HrsoutLn "Not Equal"           ' Display not equal on a serial terminal
  EndIf

  MyString = "EGGS"               ' Pre-load String MyString with the text EGGS
  If MyString = CodeString Then    ' Is MyString equal to CodeString?
    HrsoutLn " Equal "             ' Yes. So display equal on a serial terminal
  Else                              ' Otherwise…
    HrsoutLn "Not Equal "           ' Display not equal on a serial terminal
  EndIf
' String comparisons using Select-Case
  Device = 24FJ64GA002             ' Select the device to compile for
  Declare Xtal = 16        ' Tell the compiler the device will be running at 16MHz
  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyString as String * 20      ' Create a String for 20 characters

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyString = "EGGS"               ' Pre-load String MyString with the text EGGS
  Select MyString                  ' Start comparing the string
    Case "EGGS"                    ' Is MyString equal to EGGS?
      HrsoutLn "Found EGGS"
    Case "BACON"                    ' Is MyString equal to BACON?
      HrsoutLn "Found BACON"
    Case "COFFEE"                  ' Is MyString equal to COFFEE?
      HrsoutLn "Found COFFEE"
    Case Else                       ' Otherwise…
      HrsoutLn "No Match"           ' Displaying no match
  EndSelect

See also : Creating and using Strings Creating and using code memory strings If-Then-Else-EndIf, Repeat-Until Select-Case, While-Wend .

View original PDF page 70Open page in PDFOriginal PDF page 70