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 transformstring1 into string2. Returns 0 if the strings are identical.
Examples
Example 1: Basic String Comparison
Example 2: Finding Similar User Names
Example 3: Fuzzy Matching with Multiple Candidates
Notes
- Handles
nullvalues 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
nullvalues return distance of0 - Optimized for performance with memory-efficient implementation
- Case-sensitive comparison (use
toLower()if case-insensitive matching is needed)
See Also
- sim.jaccard - Set-based similarity for collections
- text.jaroWinkler - Alternative string similarity metric
Frequently Asked Questions
What does a Levenshtein distance of 0 mean?
What does a Levenshtein distance of 0 mean?
A distance of 0 means the two strings are identical — no edits are needed.
Is flex.text.levenshtein case-sensitive?
Is flex.text.levenshtein case-sensitive?
Yes. Uppercase and lowercase characters are considered different. Wrap inputs with
toLower() for case-insensitive comparison.