Thursday, October 1, 2009

Count number of specific char in a string in Java

This code will count the number of commas in a string.

String s = "Count, the number,, of commas.";
int numberOfCommas = s.replaceAll("[^,]","").length();