VBspeed / Language / IsSameText
VBspeed © 2000-10, updated: 13-Aug-2004
IsSameText


IsSameText
VB5/6 provides the StrComp function for textual comparison of two strings, and it does a good job in string sorting business. But when you just want to know whether the strings are identical or not, is StrComp the weapon of choice?
IsSameText is defined in analogy to StrComp, however it returns a boolean:
Function IsSameText(String1 As String, _
                    String2 As String, _
                    Compare As VbCompareMethod) As Boolean
Note that IsSameText is not a StrComp emulation. StrComp is just the best thing native VB offers for the IsSameText job. See notes.
Code
StrComp fRet = (StrComp(sDum1, sDum2, vbTextCompare) = 0)
lstrcmpi Public Declare Function lstrcmpi Lib "kernel32" Alias "lstrcmpiA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
fRet = (lstrcmpi(sDum1, sDum2) = 0)
IsSameText01
Public Function IsSameText01(String1 As String, _
                             String2 As String, _
                             Compare As VbCompareMethod) As Boolean
' by Donald, donald@xbeat.net, 20001012, rev 001 20040813
  If LenB(String1) = LenB(String2) Then
    If LenB(String1) = 0 Then
      IsSameText01 = True
    Else
      ' InStrB does not work with vbTextCompare
      IsSameText01 = (InStr(1, String1, String2, Compare) <> 0)
    End If
  End If
End Function
IsSameText02
Public Function IsSameText02(String1 As String, _
                             String2 As String, _
                             Compare As VbCompareMethod) As Boolean
' by Donald, donald@xbeat.net, 20001012, rev 001 20040813
  If LenB(String1) = LenB(String2) Then
    ' take the best of each worlds
    If Compare = vbBinaryCompare Then
      If LenB(String1) = 0 Then
        IsSameText02 = True
      Else
        IsSameText02 = (InStrB(1, String1, String2, Compare) <> 0)
      End If
    Else
      IsSameText02 = (StrComp(String1, String2, Compare) = 0)
    End If
  End If
End Function
IsSameText03 IsSameText03 is wrapped in a class. Cinemascope here, preview here:

Calls
Compare = vbTextCompare
1"xxx..." vs. "XXX...", both strings 1000 chars, all chars equal textwise
2"xxx..." vs. "YXX...", both strings 1000 chars, first char differs
3"xxx...x" vs. "XXX...Y", both strings 1000 chars, last char differs
4"xxx..." vs. "XXX...", 1000 vs. 999 chars, all chars equal textwise
Charts How to read all those numbers
 VB5 Charts
CodeAuthorDopingNotes
StrComp Donald  
lstrcmpi JasonAPI 
IsSameText01 Donald  
IsSameText02 Donald  
IsSameText03 ChrisAPI 
Call 1
311.09371s
25.74192s
516.83563s
411.10371s
11.0033s
Call 2
2408.81123.114s
4484.64145.953s
51,840.04554.138s
3410.29123.562s
11.000.301s
Call 3
311.16373s
25.76193s
516.86563s
411.16373s
11.0033s
Call 4
52,884.02370.889s
41,487.03191.234s
21.020.131s
11.000.129s
31.400.180s
 VB6 Charts
CodeAuthorDopingNotes
StrComp Donald  
lstrcmpi JasonAPI 
IsSameText01 Donald  
IsSameText02 Donald  
IsSameText03 ChrisAPI 
Call 1
315.94533s
25.73192s
517.28578s
415.96534s
11.0033s
Call 2
3970.67283.917s
2496.93145.350s
51,893.93553.967s
4972.28284.387s
11.000.292s
Call 3
315.96533s
25.75192s
517.31578s
415.99534s
11.0033s
Call 4
55,228.01571.065s
41,755.31191.736s
21.120.123s
11.000.109s
31.490.163s
Conclusions
  • Comparing IsSameText01 with IsSameText02 the former loses everywhere so we can just forget about it. Learn: InStr is not as good with textual comparison as it is with binary comparison (cf. Same String?).
  • IsSameText02 now looks a clear winner: although it involves a function call while the rivaling StrComp is in-line there's hardly any difference with same-length strings (Call 1 to 3), and with different-length strings IsSameText02 beats the hell out of StrComp (Call 4). I mean, 3000 to 5000 times faster sounds like convincing...
  • Note, how StrComp got considerably slower in VB6.
  • 11-Sep-2001: the lstrcmpi API is clearly best with same-length strings. Good to know.
  • 06-Dec-2001: IsSameText03 happened ... looks like a real alternative.
The bottom line is:
  1. Use IsSameText03 to find out whether two strings are textually identical.
The bottom line was:
  1. Use IsSameText02 to find out whether two strings are textually identical.
  2. By the way: IsSameText02 is also a good choice for binary comparison.
NOTES
Note that IsSameText is not about emulating StrComp! StrComp is an animal of sorting, and thus has to deal with the irregularities of human language orthography and lexical sorting standards. For example (InStr can do this trick, too):
StrComp("Masse", "Mae", vbTextCompare) --> 0 (equal)
InStr(1, "Masse", "Mae", vbTextCompare) --> 1
However, check out this one:
InStr(1, "as", "a", vbTextCompare) --> 0 (not found: 'a'='ass' is not in 'as')
InStr(1, "a", "as", vbTextCompare) --> 1 (found as pos 1: 'as' is in 'a'='ass')
So, in textual comparison mode, "" is first transliterated to "ss", and then the search begins. The same holds for all characters with an alternative digraphemic transliteration:
""     = "SS","Ss","sS","ss"
"","" = "OE","Oe","oE","oe"
"","" = "AE","Ae","aE","ae"
"","" = "TH","Th","tH","th"
Making notes on StrComp, here's a last one: VB5's StrComp sorts the number characters "1", "2", "3" (Ascii 49,50,51) *before* their superscript counterparts, whereas VB6 does not make any difference:
VB5 StrComp: 1(49) < (185), 2(50) < (178), 3(51) < (179)
VB6 StrComp: 1(49) = (185), 2(50) = (178), 3(51) = (179)
Got comments?

top




VBspeed © 2000-10 by Donald Lessau