The Levenshtein distance between two strings is a measurement of similarity. The smaller the distance, the more similar two strings are. Our PHP programmer friends have a function to calculate this distance; we deserve one too.

#### ASP

`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`

## No comments:

Post a Comment