I searched for method to calculate how many times a character is in String and found a good solution
temp.length() - temp.replaceAll("T", "").length()
here,we are calculating the number of time 'T' is in temp...
Problem is,it is not working properly for '.'
Code:
public static void main(String[] args) {
String temp="TTT..####";
System.out.println(temp.length() - temp.replaceAll("T", "").length());
System.out.println(temp.length() - temp.replaceAll(".", "").length());
System.out.println(temp.length() - temp.replaceAll("#", "").length());
}
OutPut:
run:
3
9
4
BUILD SUCCESSFUL (total time: 1 second)
according to output '.' is 9 times in String.through loop it's gives the right answer. What is the problem??