-
Couldn't load subscription status.
- Fork 0
ProtoString distance() Method
Seth Clydesdale edited this page May 28, 2015
·
5 revisions
<< Back to Miscellaneous Methods
The following example returns 3, because it takes 3 edits to turn "Kitten" into "Sitting."
'Kitten'.distance('Sitting'); // returns 3Here'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 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.
String.distance(str, i);| 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. |
Home • History • ProtoString • Created by Seth Clydesdale