Advertisement
ASP_Volume2 String Manipulation #35964

Compare 2 Strings

This VERY short function compares 2 strings and returns a number (that can be converted into percentage if multiplied by 100) that represents how closely related 2 strings are. For instance "ABCDE" and "ABCDF" would return say.... .8 (80%). Great for suggesting fixes for spelling errors et cetera. Feel free to use, abuse, and manipulate the code however you want, I'm sure it's not original, but I know it can be helpful :).

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
Public Function CompareTXT(String1 As String, String2 As String) As Single
Dim i, y, x As Integer
Dim a, b As String
String1 = UCase(String1)  'take this out if you
String2 = UCase(String2)  'want it to be case
              'sensitive
If String1 = String2 Then CompareTXT = 1: Exit Function
              'if the strings are
              'the same, don't
              'bother to waste time
              'and space on working
              'them out :).
                
If Len(String1) > Len(String2) Then x = Len(String1)
If Len(String2) > Len(String1) Then x = Len(String2)
If Len(String1) = Len(String2) Then x = Len(String1)
              'find out the length
              'of the longest string
              
For i = 1 To x
  a = Mid(String1, i, 1) 'get 1 character from
  b = Mid(String2, i, 1) 'each string and compare
  If a = b Then y = y + 1 'the characters
Next
CompareTXT = y / x
End Function
Původní komentáře (3)
Obnoveno z Wayback Machine