String comparison: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
Line 968:
 
All character comparisons are literal: case counts. There is no facility for case insensitive comparison (though in principle a compiler could offer to do so via non-standard mnemonics) and there often are no library routines for case conversion. The usual procedure is to copy both items to scratch variables (with all the annoyance of "how big?"), convert both to upper case (or both to lower case) and then compare. Or, rather than copying the strings, one might code for a character-by-character comparison and handling case differences one character at a time. With single-character comparison one can use ICHAR(''c'') to obtain the numerical value of the character code, and this enables the use of the three-way test of the arithmetical-IF as in <code>IF (ICHAR(TEXT1(L:L)) - ICHAR(TEXT2(L:L))) ''negative'',''equal'',''positive''</code>, where ''negative'' would be the statement label jumped to should character L of TEXT2 be greater than character L of TEXT1. With equality, one would increment L and after checking that TEXT1 and TEXT2 had another character L available, test afresh. Further, to accommodate case insensitivity, one could prepare an array of 256 integers, say UC, where UC(''i'') = ''i'' except for the indices corresponding to the character code values of the lower case letters which instead have those of the upper case letters. Then the comparison might be <code>IF (UC(ICHAR(TEXT1(L:L))) - UC(ICHAR(TEXT2(L:L)))) ''negative'',''equal'',''positive''</code> whereby case insensitivity is achieved without the annoyance of multiple testing or case conversion but at the cost of array access. And by re-arranging values in array UC, a custom ordering of the character codes could be achieved at no extra cost.
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0
 
' Strings in FB natively support the relational operators which compare lexically on a case-sensitive basis.
' There are no special provisions for numerical strings.
' There are no other types of string comparison for the built-in types though 'user defined types'
' can specify their own comparisons by over-loading the relational operators.
 
Function StringCompare(s1 As Const String, s2 As Const String, ignoreCase As Boolean = false) As String
Dim As String s, t ' need new string variables as the strings passed in can't be changed
If ignoreCase Then
s = LCase(s1)
t = LCase(s2)
Else
s = s1
t = s2
End If
If s < t Then Return " comes before "
If s = t Then Return " is equal to "
Return " comes after "
End Function
 
Dim As Integer result
Dim As String s1, s2, s3
s1 = "Dog" : s2 = "Dog"
Print s1; StringCompare(s1, s2); s2
s2 = "Cat"
Print s1; StringCompare(s1, s2); s2
s2 = "Rat"
Print s1; StringCompare(s1, s2); s2
s2 = "dog"
Print s1; StringCompare(s1, s2); s2
Print s1; StringCompare(s1, s2, True); s2; " if case is ignored"
s1 = "Dog" : s2 = "Pig"
s3 = StringCompare(s1, s2)
If s3 <> " is equal to " Then
Print s1; " is not equal to "; s2
End If
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
c:\FreeBasic>stringcompare
Dog is equal to Dog
Dog comes after Cat
Dog comes before Rat
Dog comes before dog
Dog is equal to dog if case is ignored
Dog is not equal to Pig
</pre>
 
=={{header|F_Sharp|F#}}==
9,490

edits