VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Compare 2 Strings

by Josh Simmons (3 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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 :).

Inputs
CompareTXT(String1 as String, String2 as String) String1 and String2 are the only paramaters you need to send to the function, those are the two strings you wish to compare.
Code Returns
% value that represents exactly how similar two strings are.

Rate Compare 2 Strings

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

Download this snippet    Add to My Saved Code

Compare 2 Strings Comments

No comments have been posted about Compare 2 Strings. Why not be the first to post a comment about Compare 2 Strings.

Post your comment

Subject:
Message:
0/1000 characters