- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to count letters in a String
Let’s say we have the following string, that has some letters and numbers.
String str = "9as78";
Now loop through the length of this string and use the Character.isLetter() method. Within that, use the charAt() method to check for each character/ number in the string.
for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) count++; }
We have set a count variable above to get the length of the letters in the string.
Here’s the complete example.
Example
public class Demo { public static void main(String []args) { String str = "9as78"; int count = 0; System.out.println("String: "+str); for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) count++; } System.out.println("Letters: "+count); } }
Output
String: 9as78 Letters: 2
Let us see another example.
Example
public class Demo { public static void main(String []args) { String str = "amit"; int count = 0; System.out.println("String: "+str); for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) count++; } System.out.println("Letters: "+count); } }
Output
String: amit Letters: 4
- Related Articles
- Java Program to count vowels in a string
- Java program to count words in a given string
- Java Program to count all vowels in a string
- Java program to count occurrences of a word in string
- How to get the maximum count of repeated letters in a string? JavaScript
- Java Program to count the number of words in a String
- Java Program to get random letters
- How to find If a given String contains only letters in Java?
- Java program to count upper and lower case characters in a given string
- C# Program to count vowels in a string
- Java program to count the occurrence of each character in a string using Hashmap
- Python program to calculate the number of digits and letters in a string
- C# program to Count words in a given string
- Python program to Count words in a given string?
- How to count the number characters in a Java string?

Advertisements