Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Command: Compare two strings for equality.

((PUBPRI))
   STRCOMP (StringAddress1, StringAddress2 )

...

Returns: TRUE if both strings are equal, FALSE otherwise.

  • StringAddress1 is an expression specifying the starting address of the first string to compare.
  • StringAddress2 is an expression specifying the starting address of the second string to compare.

...

...

Explanation

STRCOMP is one of two commands (STRCOMP and STRSIZE) that retrieve information about a string. STRCOMP compares the contents of the string at StringAddress1 to the contents of the string at StringAddress2, up to the zero-terminator of each string, and returns TRUE if both strings are equivalent, FALSE otherwise. This comparison is case-sensitive.

...

Using STRCOMP

The following example assumes PrintStr is a method created elsewhere.

Code Block
PUB Main

...


  if strcomp(@Str1, @Str2)

...


    PrintStr(string("Str1 and Str2 are equal"))

...


  else
    PrintStr(string("Str1 and Str2 are different"))

...



DAT
  Str1 byte "Hello World", 0

...


  Str2 byte "Testing.", 0 

The above example has two zero-terminated strings in the DAT block, Str1 and Str2. The Main method calls STRCOMP to compare the contents of each string. Assuming PrintStr is a method that displays a string, this example prints "Str1 and Str2 are different" on the display.

...

Zero-Terminated Strings

The STRCOMP command requires the strings being compared to be zero-terminated; a byte equal to 0 must immediately follow each string. This practice is quite common and is recommended since most string-handling methods rely on zero terminators.