detect differences between two strings with Javascript

前端 未结 2 1349
情深已故
情深已故 2020-11-29 08:12

With Javascript, I want to check how many differences there are between two strings.

Something like:

var oldName = \"Alec\";
var newName = \"Alexand         


        
2条回答
  •  有刺的猬
    2020-11-29 09:08

    I don't have a Javascript implementation on hand per se, but you're doing something for which well-established algorithms exist. Specifically, I believe you're looking for the "Levenshtein distance" between two strings -- i.e. the number of insertions, substitutions and deletions (assuming you are treating a deletion as a change).

    The wikipedia page for Levenshtein distance has various pseudo-code implementations from which you could start, and references which may also help you.

提交回复
热议问题