- 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
Check if a string contains only alphabets in Java using ASCII values
Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −
for (int i = 0; i < myStr.length(); i++) { char c = myStr.charAt(i); if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) { return false; } return true; }
Following is an example to check if a string contains only alphabets in Java using ASCII values;
Example
class Main { public static boolean checkAlphabet(String myStr) { for (int i = 0; i < myStr.length(); i++) { char c = myStr.charAt(i); if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) { return false; } } return true; } public static void main(String[] args) { String str1 = "Tom1"; System.out.println("String1 = " + str1); System.out.println("Is String1 contains only alphabets? = " + checkAlphabet(str1)); String str2 = "Tim"; System.out.println("String2 = " + str2); System.out.println("Is String2 contains only alphabets? = " + checkAlphabet(str2)); } }
Output
String1 = Tom1 Is String1 contains only alphabets? = false String2 = Tim Is String2 contains only alphabets? = true
- Related Articles
- Check if a string contains only alphabets in Java using Regex
- Check if a string contains only alphabets in Java using Lambda expression
- Is it possible to check if a String only contains ASCII in java?
- Check if the String contains only unicode letters in Java
- Check if a string contains a number using Java.
- How to check if a character column only contains alphabets in R data frame?
- Java Program to check if the String contains only certain characters
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters or digits in Java
- How to check if a Python string contains only digits?
- How to check if a string contains only decimal characters?
- Check if the String contains only unicode letters, digits or space in Java
- Python Program to check if String contains only Defined Characters using Regex
- Check whether the String contains only digit characters in Java
- How to check if a string only contains certain characters in Python?

Advertisements