Skip to main content

Description

Computes the Levenshtein edit distance between two strings. The edit distance is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another. This is useful for fuzzy string matching, spell checking, and finding similar records.

Syntax

Parameters

Returns

Type: number (integer) The minimum number of single-character edits needed to transform string1 into string2. Returns 0 if the strings are identical.

Examples

Example 1: Basic String Comparison

Output:

Example 2: Finding Similar User Names

Example 3: Fuzzy Matching with Multiple Candidates

Notes

  • Handles null values gracefully by treating them as empty strings
  • The function is symmetric: levenshtein(a, b) = levenshtein(b, a)
  • Empty strings return the length of the non-empty string as distance
  • Two null values return distance of 0
  • Optimized for performance with memory-efficient implementation
  • Case-sensitive comparison (use toLower() if case-insensitive matching is needed)

See Also

Frequently Asked Questions

A distance of 0 means the two strings are identical — no edits are needed.
Yes. Uppercase and lowercase characters are considered different. Wrap inputs with toLower() for case-insensitive comparison.