Solved

count number of results in a string search

Posted on 2012-04-13
7
176 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

626 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