To count letters in a
String
in Java, call Character.isLetter(string.charAt(i))
in a for
loop.Here's how you do it:
String string = "Hello World!";
int letterCount = 0;
for (int i = 0; i < string.length(); i++) {
if (Character.isLetter(string.charAt(i))) {
letterCount++;
}
}
System.out.println(letterCount); // 10