## Posted By

Scooter on 10/18/08

# Levenshtein distance

/ Published in: ASP   Calculate the Levenshtein distance between two strings.

`<%    ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.    '    ' This work is licensed under the Creative Commons Attribution License. To view    ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or    ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California    ' 94305, USA.     ' Compute the Levenshtein distance between two strings.    function levenshtein(byVal first, byVal second)        dim distance        dim truncateLength         if first = second then            ' The distance is zero if the strings are identical.            distance = 0        else            ' The distance is at least the difference of the lengths of the two strings.            distance = abs(len(first) - len(second))             ' Force the strings to be the same length to prevent overflows.            truncateLength = ((len(first) + len(second)) - distance) / 2            first = Left(first, truncateLength)            second = Left(second, truncateLength)             ' Compare the corresponding characters in each string.            for i = 1 to truncateLength                if Mid(first, i, 1) <> Mid(second, i, 1) then                    distance = distance + 1                end if            next        end if         levenshtein = distance    end function    %>`