wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to know the no of occurance in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to know the no of occurance in a string" Watch "how to know the no of occurance in a string" New topic
Author

how to know the no of occurance in a string

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
I always wonder how to achieve this.
Sya I have a string xx = "This is a test for find character t in it". And I want to know the number of occurance for character 't' in this. I am not able to find any api for same. but rather use a while loop aganst character 't' and put the no of occrance in some int variable.
I want to know if there is any API method for same. My methodology always takes a lot of time if file is too long.
Any suggestion.....?
regards,
Arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Here's an idea at least...
You could create a new StringTokenizer using your string and the character you want to count as the delimiter. Then count the tokens, countTokens(). Note: if the string begins or ends with the specified delimiter, then 1 would have to be added to the count for each case.
[ March 12, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
A StringTokenizer will be hard to use for something like this - whenever there are several consecutive delimiters, they are treated as a single delimiter. There's no way you can tell from the StringTokenizer how many delimiters there really were.
Really, a while loop should be the fastest way to do this. If you find it's taking too long, perhaps there's another problem. You mention a file - if you're reading from a file, that will almost certainly take more time than counting characters is a while loop. Perhaps you should post some code to show what you're doing.


"I'm not back." - Bill Harding, Twister
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
"...whenever there are several consecutive delimiters, they are treated as a single delimiter."
Oops.
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Hi,
In the StringTokenizer class, one of the constructor
StringTokenizer(String str, String delim, boolean returnDelims)
When you call countTokens(), this one will return
total tokens including the delim.
So I wrote up a small test program to test my
assumption.
StringTokenizer st1 = new StringTokenizer(a, "a", true);
StringTokenizer st2 = new StringTokenizer(a, "a");
int total = st1.countTokens() - st2.countTokens()
total is the number of "a" in the string.

I tried quite a few cases. and it seems to be the case.
But I am not sure. You could try it.
If anyone know if this is wrong, feel free to point it out.

kawaii
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You're quite right, kawaii. I misspoke when I said there's no way you can tell from the StringTokenizer how many delimiters there really were. Well, there's no way to tell from just one StringTokenizer. And it will be much more efficient and straightforward to just count up the characters in a while loop:

[ March 12, 2002: Message edited by: Jim Yingst ]
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
try this one....


in the countAppearence method always at the time of invoke plz pass first and last arg's 0.
Ex.
System.out.println(xyz.countAppearence(0,"Gautam Shah ","a",0));
System.out.println(xyz.countAppearence(0,"Arun Mahajan ","n",0));

xyz is the name of the class in which this proc exists.
[ March 13, 2002: Message edited by: gautam shah ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to know the no of occurance in a string
 
Similar Threads
Strip Letters From End of String
Trouble crafting regular expression.
StringBuffer Question
trouble with operator
regex question