dcsimg
What is the easiest way to count the number of occurences of a certain String inside another string?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   neal_ravindran
Posted On:   Sunday, June 1, 2003 06:09 AM

I have a string with "[" (ie forward facing brackets) and "]" (ie backward facing brackets). Many of these occur all thru the string...in random order. Idea is to make them both be equal. So in my function to make them both be equal...I need to count the number occurences of both fwd and backward facing brackets.... example:-myString="[who]let[The[Dogs]]ou]t]"; What is the easiest way to count the number of occurences of a certain String inside another string? Was gonna use myString.toCharArray() and then loop thru to get the number of '[' for example...Likewise for ']'. (I think I might have to wrap it as java.lang.Character object to do the comparison here...) Looks drawn out to me...Is that the eas   More>>

I have a string with "[" (ie forward facing brackets) and "]" (ie backward facing brackets). Many of these occur all thru the string...in random order. Idea is to make them both be equal. So in my function to make them both be equal...I need to count the number occurences of both fwd and backward facing brackets....

example:-myString="[who]let[The[Dogs]]ou]t]";


What is the easiest way to count the number of occurences of a certain String inside another string? Was gonna use myString.toCharArray() and then loop thru to get the number of '[' for example...Likewise for ']'. (I think I might have to wrap it as java.lang.Character object to do the comparison here...) Looks drawn out to me...Is that the easiest to count the occurrences?

   <<Less

Re: What is the easiest way to count the number of occurences of a certain String inside another string?

Posted By:   Dover_Martin  
Posted On:   Sunday, June 1, 2003 02:32 PM

This sounds remarkably like homework from a comp sci class... :-)

Re: What is the easiest way to count the number of occurences of a certain String inside another string?

Posted By:   Jeanne_Boyarsky  
Posted On:   Sunday, June 1, 2003 10:09 AM

It's not really neccessary to convert to an array. You could just use myString.charAt(), which also avoids the need to wrap.


Is counting enough for what you are doing? Is ']]hi[[' acceptable?
About | Sitemap | Contact