Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

count number of results in a string search

Posted on 2012-04-13
7
Medium Priority
?
179 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Assisted Solution

by:JT92677
JT92677 earned 200 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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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 discusses how to create an extensible mechanism for linked drop downs.
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…

715 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