Home Tutorial Java Corejava Count letters in a string.

 
 

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

Advertisement

Related Tags for Count letters in a string.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 28, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com