Solved

count number of results in a string search

Posted on 2012-04-13
7
173 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Find closest specified item from present item - Javascript 5 34
multiple selects 23 49
I'm a Human captcha checkbox 13 23
Clear input text 15 8
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

820 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