I
I
iDennis2014-07-30 16:59:18
JavaScript
iDennis, 2014-07-30 16:59:18

How to quickly determine differences in a string?

There are two lines:
1. Hello
2. He_ll_o
How to determine where the words differ (character number)? What are fast algorithms?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey, 2014-07-30
Protko @Fesor

https://github.com/kpdecker/jsdiff

A
Anna Bakurova, 2014-07-30
@Libris

write a loop and check the correctness and compliance of the character through indexOf ()

B
buzzi888, 2014-07-30
@buzzi888

Fuzzy search, materiel Fuzzy search in text and dictionary

S
Sergey Paramonov, 2014-07-31
@varagian

Levenshtein distance

X
xmoonlight, 2014-08-30
@xmoonlight

function compare(a,b) {
 if(a==b) return 0;
 var m=Math.min(a.length,b.length);
 var r=m+1;
 if (m>0) {
  for (var i=0;i<m;i++) 
    if (a[i]!=b[i]) {
         r=i+1;
         break;
    }
 } else r=1;
 return r;
}

var a='Hello';
var b='He_ll_o';

document.write(compare(a,b)); //3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question