Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string search, can it be done faster ?

Posted on 2012-04-03
7
Medium Priority
?
178 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Rozamunda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37804552
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
 

Author Comment

by:Rozamunda
ID: 37804563
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
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37804582
<<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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rozamunda
ID: 37804595
ok, so you think is it faster than index of  ? My strings can be very long, max 10000 characters
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37804609
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
 

Author Comment

by:Rozamunda
ID: 37804617
But I am passing a second parameter to the indexof telling when to begin the next search (if match was found before)
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 37804641
<<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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question