string search, can it be done faster ?

i am looking for occurrances of the word 'checked' and adding it to an array
var strg;
strg += "Lorem ipsum dolor sit amet";
strg + = "checked";
strg += "Lorem ipsum dolor sit amet";
strg += "Lorem ipsum dolor sit amet";

   k = strg.indexOf("checked");
   
   var dupa = [];
   
  while (k !=  -1)
  {
	  dupa.push(k);
	 k = strg.indexOf("checked",k+10);
	 
   }
  

Open in new window

RozamundaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Gurvinder Pal SinghConnect With a Mentor Commented:
<<But I am passing a second parameter to the indexof telling when to begin the next search (if match was found before)>>
So, how does it become faster than split()? It is just like writing more code to do the same thing
0
 
Gurvinder Pal SinghCommented:
if you simply want to check the number of occurrences of String "checked", then just try this

var strg;
strg += "Lorem ipsum dolor sit amet";
strg + = "checked";
strg += "Lorem ipsum dolor sit amet";
strg += "Lorem ipsum dolor sit amet";

var numberOfInstances =  strg.split("checked").length - 1;


var dupa = [];
for ( var counter = 0; counter < numberOfInstances ; counter++)
{
    dupa[counter] = "checked";
}
0
 
RozamundaAuthor Commented:
not really, actually I want to add to an array the word which is after checked, and starts with '978..'. But I am going to using another index of after I find 'checked' (if this is the most optimal way)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Gurvinder Pal SinghCommented:
<<not really, actually I want to add to an array the word which is after checked, and starts with '978..'. But I am going to using another index of after I find 'checked' (if this is the most optimal way)>>
I don't see this in your original question.

You already have an array which is splitted by "Checked", so you know that every item of the array is the word coming after "checked" only. So you can add accordingly
0
 
RozamundaAuthor Commented:
ok, so you think is it faster than index of  ? My strings can be very long, max 10000 characters
0
 
Gurvinder Pal SinghCommented:
everytime you do index of, whole string has to be searched again for the parameter you will be passing for indexOf()

Split() is likely to do the same in one iteration of the characters in the string
0
 
RozamundaAuthor Commented:
But I am passing a second parameter to the indexof telling when to begin the next search (if match was found before)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.