This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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
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 ]
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
Joined: Dec 10, 2001
"...whenever there are several consecutive delimiters, they are treated as a single delimiter." Oops.
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.
Joined: Jan 30, 2000
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 oneStringTokenizer. And it will be much more efficient and straightforward to just count up the characters in a while loop:
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 ]
subject: how to know the no of occurance in a string