• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to know the no of occurance in a string

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...whenever there are several consecutive delimiters, they are treated as a single delimiter."
Oops.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic