Solved

count number of results in a string search

Posted on 2012-04-13
7
172 Views
Last Modified: 2012-06-21
Hello,
I have this string and I need to check how many times '<li>' is present please:

<li>aho</li><li>hao</li><li>loa</li><li>ola</li>

Open in new window


thank you
0
Comment
Question by:eMisc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 37843831
which long u r using, php or ....
0
 

Author Comment

by:eMisc
ID: 37843863
Experts Exchange- Scripting Languages- JavaScript- Count Number Of Results In A String Search
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37843867
Assuming the <li>s are inside a <ul>, then in javascript you could do this:
<body>
<ul id="ul1">
<li>aho</li><li>hao</li><li>loa</li><li>ola</li>
</ul>
<script type="text/javascript">
alert(document.getElementById("ul1").getElementsByTagName("li").length);
</script>
</body>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Assisted Solution

by:JT92677
JT92677 earned 50 total points
ID: 37843885
If you get the lengths of the substring you're looking for, and the length of the string being analyzed, then
use a Replace command, such as str_replace all occurrances of the substring with '' (nothing) in the analyzed string.

Then the number of occurrances woud be  

( (length of string before replacement) - (length of string after replacement) ) / (Length of target substring)

Kind of a generic solution if you have a function to replace a substring within a string, or write such a function
if whatever language you're using doesn't have it.  String replacements can be done in several ways, but
once the string has been emptied of the substring, the lengths before and after will tell you how many
substrings existed originally.

There might be simpler solutions, but this would work.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 250 total points
ID: 37843900
How about this?
<script type="text/javascript">
var liCount = "<li>aho</li><li>hao</li><li>loa</li><li>ola</li>".split("<li>").length - 1;
alert(liCount);
</script>

Open in new window

0
 

Author Closing Comment

by:eMisc
ID: 37843938
thank you!
0
 
LVL 8

Expert Comment

by:JT92677
ID: 37844278
eMisc -- tommyboy figured you were doing this in javascript. Okay, if so there are some useful properties and methods in javascript that you might want to check out as alternative ways to deal with strings and javascript.

For example:

    mystr = "<li>aho</li><li>hao</li><li>loa</li><li>ola</li>";

You can get the length as    lenmystr = mystr.length;

Then use replace method with the global match switch to replace all occurrences in the string

    mystr.replace("<li>"/gm, "");   to replace globally all the "<li>" found with nothing.

Now  mystr.length; property will be the length of the string without all the "<li>" substrings.

and the number of matches = (lenmystr - mystr.length) / "<li>".length

Check out http://www.w3schools.com/jsref/jsref_obj_string.asp

The /g switch means look for all (global), the /m means multi-line.

Just FYI - I realize the question is closed, but someone else may benefit, who knows.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

803 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