UserGuide

Text Comparison

From Xojo Documentation

Case Insensitive

All text comparison is case-insensitive by default. This means that text such as “Hello”, “HELLO”, “hello” and “hELLO” are all treated the same when you compare using the comparison operators (=, <, >, <>, <=, >=).

If "Hello" = "hello" Then
MsgBox("They match!") // Displayed
End If

If "Hello" <> "hello" Then
// Not displayed
MsgBox("They do not match.")
End If

Case Sensitive Comparisons

When you need to do case-sensitive text comparisons on String data, then use the StrComp function to test the text values. StrComp takes three parameters (string1, string2 and mode) and returns an Integer value indicating the result of the comparison.

The mode parameter indicates the type of comparison to do. When mode = 0, the comparison is done strictly using the bytes of the string. This means that if the string have two different encodings, they will always be different. In most cases you will want to use mode = 0.

When mode = 1, the strings are only compared for case sensitivity if they match exactly other than the case. For example, “Hello” and “HELLO” would be different strings because they are the same other than the case. However, it does mean that text such as “hello” and “Today” may not compare exactly how you would expect. In the strictest sense, “hello” is greater than “Today” because “h” has a higher character code than “T”. But with mode = 1, that detail is not used because the two strings are not the same.

Here are some examples:

If StrComp("Hello", "hello", 0) = 0 Then
// Not displayed
MsgBox("They match!")
End If

If StrComp("Hello", "hello", 0) <> 0 Then
// Displayed
MsgBox("They do not match.")
End If

Case Sensitive Comparison using the Text data type

When using the Text data type, then you can use the Compare method to do a case-sensitive comparison.

Dim d1 As Text = "Dog"
Dim d2 As Text = "dog"

Dim result As Integer
result = d1.Compare(d2, Text.CompareCaseSensitive)

// result < 0

The result value is less than 0 when d1 < d2, greater than 0 then d1 > d2 and 0 when they are they same.

See Also

String data type; StrComp function; UserGuide:Framework topic