Is there a function which returns the number of a special character in a String?
Hi all,
is there a function which returns the number of "x" from the String "ABCxxDEFxGxHIJxxx"? (==> for example)
==> should return 7
Thanx all,
Andi
Re: Is there a function which returns the number of a special character in a String?
I dont think there is a function to get the count, but a function like countPattern in the below program should help.
public class PatternCount {
public static void main(String[] args) {
String str = "ABCxxDEFxGxHIJxxx";
System.out.println(countPattern(str,"x"));
}
public static int countPattern(String str,String pattern) {
int count = 0;
for(int i=-1;(i=str.indexOf(pattern,i+1)) >= 0;count++);
return count;
}
}
rums1976
Re: Is there a function which returns the number of a special character in a String?
Hi there,
thank you for your answer.
Unfortunatelly I am missing lot´s of functions in JAVA I had while I was programming with MFC - anyway, thank you very much!
Andi