| Author |
count a particular character in a string
|
Philip Zac
Ranch Hand
Joined: Apr 27, 2008
Posts: 66
|
|
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
Joined: May 29, 2008
Posts: 445
|
|
|
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.
|
Never try to be a hard-worker. Be a smart-worker.
My Blog
|
 |
Philip Zac
Ranch Hand
Joined: Apr 27, 2008
Posts: 66
|
|
Hello Raj, I have searched the API, But there is no such thing... Philip
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
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
Joined: Nov 08, 2007
Posts: 457
|
|
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)
|
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
 |
|
|
subject: count a particular character in a string
|
|
|