• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

count a particular character in a string

 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,


Is there any way, I can get the value or number of particular character in a given string.

Like ...example abe,abc is a given String.

here there is only one ",". likewise there is 2 a's ..


hope someone can help me..

thanks

Philip
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want us to do your homework... It's not a good thing... As for your question just use String classes and it's methods to do what you need. There is a lot of methods that can help you to accomplish your thing.
 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raj,

I have searched the API, But there is no such thing...

Philip
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is quite easy. There are several ways to do it; in fact a search of JavaRanch will probably reveal old threads about similar problems.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you didn't read the API very diligently,

no, there's no "now many Xs are there" method,

but there are �
a) what's the index of X (starting at index i)
b) break the string into pieces, using X as a delimiter
c) what's the character at index i

any could be used to answer your question in several lines of code (or one line, if obfuscation is your cup of tea)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic