0

lets say I have 2 strings:

var a = "abcdef", b = "abcdefgh";

I want to find the first index that breaks the complete match without iterating over the two strings and comparing every character with loop.

In this example I want to get 6 since g is the character that breaks the full match

3

4 Answers 4

3

Using reduce:

const findIndex = (a, b) => {
    if (b.length > a.length)
        return a.length;

    a = Array.from(a);
    b = Array.from(b);

    const result = a.reduce((acc, cur, ind) => {
        if (cur != b[ind] && (typeof acc.ind == 'undefined')) {
            acc.ind = ind;
        }
        return acc;
    }, {});

    return result.ind;
}

An example:

let a1 = "abcdef";
let b1 = "abcdefgh";

const findIndex = (a, b) => {
    if (b.length > a.length)
        return a.length;
        
    a = Array.from(a);
    b = Array.from(b);
    
    const result = a.reduce((acc, cur, ind) => {
        if (cur != b[ind] && (typeof acc.ind == 'undefined')) {
            acc.ind = ind;
        }
        return acc;
    }, {});

    return result.ind;
}

console.log(findIndex(a1, b1));
console.log(findIndex('aaa', ''));
console.log(findIndex('hey:)', 'hey!'));

In addition, version with for loop. Special thanks to @Andreas for the advice about how if statements can be avoided:

const findIndex = (a, b) => {
    for (let i = 0, l = Math.max(a.length, b.length); i < l; i++) {
        if (a[i] != b[i])
            return i;
     }
     return 'equal';
}

An example:

let a = "abcdef";
let b = "abcdefgh";

const findIndex = (a, b) => {
    for (let i = 0, l = Math.max(a.length, b.length); i < l; i++) {
        if (a[i] != b[i])
            return i;
    }
    return 'equal';
}

console.log(findIndex(a, b));
console.log(findIndex('aaa', ''));
console.log(findIndex('hey:)', 'hey!'));

7
  • It's laudable that you added all those "corner cases" but if (a[i] != b[i]) { return i } would be enough. JavaScript won't throw an error when you try to access a non-existent index of a string, hence in the worst case it would be if (undefined != ...).
    – Andreas
    Dec 24, 2019 at 12:27
  • But: "without iterating over the 2 string and comparing every character with for."
    – Andreas
    Dec 24, 2019 at 12:29
  • @Andreas thanks, I've updated answer
    – StepUp
    Dec 24, 2019 at 12:31
  • If you change your for header than you can also strip the second if: for (let i = 0, l = Math.max(a.length, b.length); i < l; i++) { ... }
    – Andreas
    Dec 24, 2019 at 12:35
  • 1
    @Light please, see my updated answer
    – StepUp
    Dec 24, 2019 at 13:25
2

Try this - var index = b.indexOf(a) + a.length

var a = "abcdef", b = "abcdefgh";
document.write(b.indexOf(a) + a.length);

1
  • Hi, Welcome to SO! Since there are multiple answers already and if you're adding a new answer then you should add why do you think this is better? A working demonstration, explanation of your answer (at a very least). Also good to know: How do I write a good answer?
    – Vivek
    Dec 24, 2019 at 12:39
2

Well in this scenario try a.lastIndexOf(b.charAt(b.length));

0
1

Here's a quicker solution without loops -

var stringA = "abcdef",
  stringB = "abcdefgh",
  firstOccurrence = stringB.indexOf(stringA);

if (firstOccurrence === -1) {
  alert('Search string Not found');
} else {
  var stringALength = stringA.length;
  document.write(firstOccurrence + stringALength);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.