Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to know the no of occurance in a string

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...whenever there are several consecutive delimiters, they are treated as a single delimiter."
Oops.
 
Alex Ku
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic