Click to See Complete Forum and Search --> : Is there a function which returns the number of a special character in a String?


Andi
September 13th, 2000, 05:50 AM
Hi all,

is there a function which returns the number of "x" from the String "ABCxxDEFxGxHIJxxx"? (==> for example)

==> should return 7

Thanx all,
Andi

rums1976
September 14th, 2000, 12:00 PM
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

Andi
September 15th, 2000, 01:18 AM
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