1

I need JavaScript algorithm that can match substring of a sting?

subStringFinder('abbcdabbbbbck', 'ab')

should return index 0

and

subStringFinder('abbcdabbbbbck', 'bck') should return index 10

Could you please tell me how to write this code?

--EDIT:

Thanks to @Jonathan.Brink I wrote that code and it did the trick:

function subStringFinder(str, subString) {
  return str.indexOf(subString);
}

subStringFinder('abbcdabbbbbck', 'bck') // -> 10
John Taylor
  • 729
  • 3
  • 11
  • 22

1 Answers1

3

You are looking for the indexOf function which is available via the built-in string type (as well as array).

Example:

var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9

Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115