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.09371µs
25.74192µs
516.83563µs
411.10371µs
11.0033µs
Call 2
2408.81123.114µs
4484.64145.953µs
51,840.04554.138µs
3410.29123.562µs
11.000.301µs
Call 3
311.16373µs
25.76193µs
516.86563µs
411.16373µs
11.0033µs
Call 4
52,884.02370.889µs
41,487.03191.234µs
21.020.131µs
11.000.129µs
31.400.180µs
 VB6 Charts
CodeAuthorDopingNotes
StrComp Donald  
lstrcmpi JasonAPI 
IsSameText01 Donald  
IsSameText02 Donald  
IsSameText03 ChrisAPI 
Call 1
315.94533µs
25.73192µs
517.28578µs
415.96534µs
11.0033µs
Call 2
3970.67283.917µs
2496.93145.350µs
51,893.93553.967µs
4972.28284.387µs
11.000.292µs
Call 3
315.96533µs
25.75192µs
517.31578µs
415.99534µs
11.0033µs
Call 4
55,228.01571.065µs
41,755.31191.736µs
21.120.123µs
11.000.109µs
31.490.163µs
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", "Maße", vbTextCompare) --> 0 (equal)
InStr(1, "Masse", "Maße", 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