Skip to content

ProtoString distance() Method

Seth Clydesdale edited this page May 28, 2015 · 5 revisions

<< Back to Miscellaneous Methods

Examples

The following example returns 3, because it takes 3 edits to turn "Kitten" into "Sitting."

'Kitten'.distance('Sitting'); // returns 3

Here's some more examples, including case-sensitivity

'Sat'.distance('Pat'); // returns 1 : Substitution of "S" for "P"
'Pat'.distance('Cats'); // returns 2 : Substitution of "C" for "P", and addition of "s"
'Cats'.distance('Dogs'); // returns 3 : All letters except "s" were edited

'Cat'.distance('cat'); // returns 1, because "C" is lowercase
'Cat'.distance('cat', true); // returns 0, because the calculation was case-insensitive 

Description

The distance method is used to calculate the Levenshtein distance between two strings. It calculates how many edits, insertions, or deletions it takes to turn one string into the other, for example, it takes one edit to turn Pat into Cat. If the strings are equal, 0 is always returned.

Syntax

String.distance(str, i);

Parameters

Parameter Description
str A string that you want to calculate the Levenshtein distance between. Defaults to the string this method was executed on if left undefined.
i A boolean value specifying that you want to execute a case-insensitive calculation. Setting this parameter to true will perform a case-insensitive calculation. Defaults to case-sensitive if left undefined.
Clone this wiki locally