1

Something like indexOf, except I need to find all indexes. If there's .indexOf and ,lastIndexOf , shouldn't there be a function to get all index occurences? I couldn't find.

Note that the string is very large, about 1MB in size, so I would need the fastest solution.

To clarify, I need to get all positions of where a substring occurs in a string.

E.g.

var str = "foo bar foo bar"; //the real string is 1MB
var indexes = str.indexOfAll('foo'); //the function I need
console.log(indexes); //should print [0,8];

One thing that comes to mind is to use indexOf in a recurring loop, finds the first word, cut the string at the index, then use indexOf again and so on until it finds nothing. I'm not sure about performance (cutting and re-creating large strings).

Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67
  • `indexOf` within loop – hindmost Jun 04 '19 at 14:51
  • Yeah I just thought about that edited my answer. But is there something more optimal? – Maciej Krawczyk Jun 04 '19 at 14:52
  • I would disagree about this duplicate. It is about searching for a string in an array, but this question is about searching for a string pattern, which can be achieved using string-search algorithms like Knuth-Morris-Pratt or Boyer-Moore. – Yeldar Kurmangaliyev Jun 04 '19 at 14:55
  • _is there something more optimal?_ IMO loop with `indexOf` is most optimal. It doesn't cut/re-create strings. Furthermore, it doesn't make redundant iterations if one uses 2nd parameter of `indexOf` – hindmost Jun 04 '19 at 14:58
  • I'm going to try it and see how much it takes. I didn't know you can use indexOf in a loop just like that. If it takes a lot of time I'm going to try that string search algo. Thanks! – Maciej Krawczyk Jun 04 '19 at 15:01
  • I don't think it is a duplicate either, at least of the question that is marked now. And the previous one which is no longer there, was about finding characters in a small string. – Maciej Krawczyk Jun 04 '19 at 15:03
  • _was about finding characters in a small string._ Size (of both needle and haystack) doesn't matter. `indexOf` work with any strings. Finding a character in a string is just particular case. – hindmost Jun 04 '19 at 15:14
  • @MaciejKrawczyk I think that the basic implement ion of any language for a purpose is fastest. But I reopened the question. Lets see if you get some efficient solution. – Maheer Ali Jun 04 '19 at 15:15
  • What should happen if the string is `foo bar foo bar food` should it return [0,8,16]? – hajile78 Jun 04 '19 at 16:02
  • @MaheerAli Yes, it could be the same case. Though if I saw it correct, the top answer operated specifically on characters. Nevermind. Thanks for re-opening. I will update when I implement it. – Maciej Krawczyk Jun 04 '19 at 17:53
  • It seems that you eventually chose "`indexOf` in loop" solution. So, as I wrote, your post is assumed to be a dupe, as [similar post](https://stackoverflow.com/questions/10710345/finding-all-indexes-of-a-specified-character-within-a-string) has mostly [the same answer](https://stackoverflow.com/a/10710406/2118955). – hindmost Jun 05 '19 at 17:01

2 Answers2

5

Easy solution:

const str = "...";
const searchKeyword = "...";

const startingIndices = [];

let indexOccurence = str.indexOf(searchKeyword, 0);

while(indexOccurence >= 0) {
    startingIndices.push(indexOccurence);

    indexOccurence = str.indexOf(searchKeyword, indexOccurence + 1);
}

If you need something highly performant, you may look over specific text search/indexing algorithms like Aho–Corasick algorithm or Boyer–Moore string-search algorithm.

Really depends on your use case and if the text you're searching into is changing or is static and can be indexed beforehand for maximum performance.

zhulien
  • 5,145
  • 3
  • 22
  • 36
0

const IndexString = (str1, str2, output=[]) =>{
  for(let i = 0; i < str1.length -1; i++){
    let arr = [];
    for(let j = i; j < (i+str2.length) & i<str1.length; j++) arr.push(str1[j])
    const findnew = arr.join('');
    if(findnew===str2) output.push(i);
  }
  return output
}

console.log(IndexString("tiktok tok tok tik tok tik", "tik"))