How To Count Letters in String in Java
In the last post, we have discussed how to find character or character position in string in java with simple examples and now here we are going to create a simple example for how to count letters in java string with examples.
So let's start with a useful example for finding the number of letters in a given java String.
The below example gives you a total number of letters count in a given java string i.e (given string is "sun45tt" and letters in this give string is suntt i.e 5).
Java Program to Count Letters in String
class LettersCountInString
{
public static void main(String args[])
{
String str = "sun45tt";
int count = 0;
for(int i = 0; i<str.length(); i++)
{
if(Character.isLetter(str.charAt(i)))
{
count++;
}
}
System.out.println(count);
}
}
Output: 5
Here we discussed, how to write a java program to count letters in string with a simple example.