priyavrat
asked on
How to count occurences of a char in string?
Hi
Is there any api for counting occurences of
a character in a string.
Eg, My string is "2-3-4-5"
and i want to know the number of times "-"
appeared in the string
I know its simple but is there any api for it?
Thanks
Priyavrat
Is there any api for counting occurences of
a character in a string.
Eg, My string is "2-3-4-5"
and i want to know the number of times "-"
appeared in the string
I know its simple but is there any api for it?
Thanks
Priyavrat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think "while(*s)if(*s=='-')n++;" is just too simple to have an API for it.
Sorry, that should be "while(*s)if(*s++=='-')n++ ;"
oops, thats not java, sorry
LOL :-D
ASKER
there is nothing to LOL TimYates
ASKER
Thanks buddy.
I'm new to Java but anyway thanks again
I'm new to Java but anyway thanks again
Soz fella, I was LOL at the cool code TheSailor put in :-)
I wish we could do that in java sometimes...there should be something like:
int n = 0 ;
String s = "2-3-4-5" ;
pointeraware {
while(*s)if(*s++=='-')n++;
}
Or summat...
Wasn't laughing at you... :-/
I wish we could do that in java sometimes...there should be something like:
int n = 0 ;
String s = "2-3-4-5" ;
pointeraware {
while(*s)if(*s++=='-')n++;
}
Or summat...
Wasn't laughing at you... :-/