This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
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
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
"...whenever there are several consecutive delimiters, they are treated as a single delimiter." Oops.
Alex Ku
Ranch Hand
Joined: Jan 15, 2002
Posts: 47
posted
0
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: 18670
posted
0
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:
[ March 12, 2002: Message edited by: Jim Yingst ]
gautam shah
Ranch Hand
Joined: Oct 29, 2000
Posts: 72
posted
0
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 ]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: how to know the no of occurance in a string