简体   繁体   中英

How to find out the position of the first occurrence of the difference between the two string?

For example, Hello World! and Hi World! - the first occurrence of the difference is at the second character. What would be the JavaScript/jQuery function?

Assuming, like other answers, that matching strings return -1 :

 // Find common prefix of strings a and b. var prefix = function(a,b){ return a && a[0] === b[0] ? a[0] + prefix(a.slice(1), b.slice(1)) : ''; }; // Find index of first difference. var diff = function(a,b){ return a===b ? -1 : prefix(a,b).length; }; var tests = [ ['Hello World!', 'Hi World!'], ['aaabab', 'aaabzbzz'], ['', ''], ['abc', 'abc'], ['qrs', 'tu'], ['abc', ''], ['', 'abc'] ]; console.log('diff', tests.map(test => diff(test[0], test[1]))); // Or just count up to the first difference // Trickier nested ternary to handle the -1 however. var diff2 = function(a,b){ return a === b ? -1 : a[0] === b[0] ? 1 + diff2(a.slice(1), b.slice(1)) : 0; }; console.log('diff2', tests.map(test => diff2(test[0], test[1]))); 

Maybe something like this? It returns, in that order, the position of the first difference if there's any, the length of the shortest string if those are different, or -1 if everything is equal.

function findDiff(a, b) {
    a = a.toString();
    b = b.toString();
    for (var i = 0; i < Math.min(a.length, b.length); i++) {
        if (a.charAt(i) !== b.charAt(i)) { return i; }
    }
    if (a.length !== b.length) { return Math.min(a.length, b.length); }
    return -1;
}

Thanks Phil for the suggestions!

function firstDiff(a, b) {
    var i = 0;

    while (a.charAt(i) === b.charAt(i)) 
        if (a.charAt(i++) === '')
            return -1;

    return i;
}

Returns the position where the two strings a and b first differ or -1 if they are equal.

A more efficient but less readable version:

function firstDiff(a, b) {
    for (var i = 0, c; (c = a.charAt(i)) === b.charAt(i); ++i) 
        if (c === '')
            return -1;

    return i;
}

If you feel that you should first stringify the arguments, then do it in the invocation:

firstDiff(toString(a), toString(b))

Most often that will be a waste of time. Know your data!

function strDiff(first, second) {
    if(first==second)
        return -1;
    first  = first.toString();
    second = second.toString();
    var minLen = min(first.length,second.length);
    for(var i = 0; i<minLen; i++) {
        if(first.charAt(i) != second.charAt(i)) {
            return i;
        }
    }
    return minLen;
}

Returns -1 if the strings do not differ, or the index (starting at 0) of the character at which they do (this is the length of the shortest string if they only differ by being different lengths, eg 'abcd' and 'abcdef' would return 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM