KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: String comparison
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: July 2, 2002

Compatibility: 6.5.x, 6.7.x, and 6.8.x

There are two ideal ways to compare strings:


- Non-case-sensitive comparison

- Case-sensitive comparison


The non-case-sensitive comparison compares two strings regardless of the character case status (uppercase vs. lowercase). In this type of comparison, the strings "HELLO" and "Hello" are considered to be a match.


For example:

If (Length($String1)=Length($String2))

 If ($String1=$String2)

  $b_SameString:=True ` Same


 Else


  $b_SameString:=False ` Not the same

 
End if



End if


If $String1 is "HELLO" and $String2 is "Hello", $b_SameString will set to True.


For the case-sensitive comparison, the ASCII value of each character in the strings must be compared instead. Since an uppercase character returns a different ASCII value than its lowercase character, the comparison will tell us immediately whether the strings are identical.


For example:

If (Length($String1)=Length($String2))

 For ($i;1;Length($String1))

  If (Ascii($String1[[$i]])#Ascii($String2[[$i]]))

   $b_SameString:=False ` Not the same

   $i:=Length($String1)+1 ` Break out

  
End if


 
End for



End if


If $String1 is "HELLO" and $String2 is "Hello", $b_SameString will set to False.