Home Tutorial Java Corejava Count letters in a string.

 
 

Count letters in a string.
Posted on: June 28, 2010 at 12:00 AM
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

Related Tags for Count letters in a string.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.