StrComp (Compare Strings)

The StrComp function is used to compare two strings, generally for the purpose of determining if the strings are identical or to determine their sorting order. StrComp is case sensitive.

Syntax

Variable = StrComp ( String1, String2 )

Remarks

The StrComp instruction is typically used to determine the sorting order of two strings. StrComp can also be used to simply determine if the strings are identical, but that task is more commonly accomplished with the logical expression of “If String1 = String2 Then / EndIf”.

Starting with the first character in each string, the characters in String2 are subtracted from the characters in String1 until the difference is non-zero or until the end of String2 is reached. The result of this instruction is an integer in the range of -255 to +255. If 0 is returned, the strings are identical. StrComp is case sensitive.

If StrComp Returns Example
String1 sorts ahead of String2 -1 to -255 StrComp(“bees”,”knees”) = -9
String1 is equal to String2 0 StrComp(“same”,”same”) = 0
String1 sorts after String2 1 to 255 StrComp(“2015-03-12”,”2015-03-11”) = 1

String variables can be declared as only one or two dimensions; for example, String(x) or String(x,y). To begin reading or modifying a string at a particular location into the string, enter the location as a third dimension; for example, String(x,y,n) where n is the desired character. For example, given an array of strings Str(10,10), Str(2,2,n) refers to n character in the (2,2) element of the array. Use Str(1,1,n) for a scalar variable and Str(x,1,n) for a one dimensional array element.

NOTE: String functions are case sensitive. Uppercase or Lowercase can be used to convert to all one case prior to processing the string if desired.