Find difference between two strings in JavaScript

后端 未结 3 860
暖寄归人
暖寄归人 2020-12-20 18:45

I need to find difference between two strings.

const string1 = \'lebronjames\';
const string2 = \'lebronnjames\';

The expected output is t

3条回答
  •  情深已故
    2020-12-20 19:10

    this will return the first difference between two string

    Like for lebronjames and lebronnjames is n

    const string1 = 'lebronjames';
    const string2 = 'lebronnjabes';
    
    
    const findFirstDiff = (str1, str2) =>
      str2[[...str1].findIndex((el, index) => el !== str2[index])];
    
    
    // equivalent of 
    
    const findFirstDiff2 = function(str1, str2) {
      return str2[[...str1].findIndex(function(el, index) {
        return el !== str2[index]
      })];
    }
    
    
    
    console.log(findFirstDiff2(string1, string2));
    console.log(findFirstDiff(string1, string2));

提交回复
热议问题