number special characters code example Example: counting special characters int count = str.length() - str.replaceAll("\\$","").length();