Count letters in a string.


 

Count letters in a string.

This example counts letter present in string.

This example counts letter present in string.

Description:

This tutorial demonstrate how to find number of letter exists in a string. The str.length() method find the length of string.

Code:

public class CheckLetterInString {
  public static void main(String[] args) {
    String str = "1a2b3c4d5e678f";
    int counter = 0;
    for (int k = 0; k < str.length(); k++) {
      if (Character.isLetter(str.charAt(k)))
        counter++;
    }
    System.out.println(counter + " letters.");
  }
}

Output:

Download this code

Ads