Check if a String Contains Only Alphabets in Java using ASCII Values Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse the whole string and land up on finding all characters in a string within this domain pool then the string is surely only alphabetic. Examples: Input: GeeksforGeeksOutput: True Input: Geeks4GeeksOutput: False Input: nullOutput: False In this article, the characters of the string are checked one by one using their ASCII values. Algorithm: Get the stringMatch the string: Check if the string is empty or not. If empty, return falseCheck if the string is null or not. If null, return false.If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.Return true if matchedExample 1: Java // Java Program to Check if a String Contains Only Alphabets // Using ASCII values // Importing required classes import java.util.*; // Class 1 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of helper class Helper h = new Helper(); // Print statement System.out.println( h.isStringOnlyAlphabet("geeksforgeeks")); } } // Class 2 // Helper class class Helper { public static boolean isStringOnlyAlphabet(String str) { // If string is empty or null if (str == null || str.equals("")) { // Return false return false; } // If we reach here we have character/s in string for (int i = 0; i < str.length(); i++) { // Getting character at indices // using charAt() method char ch = str.charAt(i); if ((!(ch >= 'A' && ch <= 'Z')) && (!(ch >= 'a' && ch <= 'z'))) { return false; } } // String is only alphabetic return true; } } OutputtrueComplexity Analysis: Time Complexity: O(N), where N is the length of the string.Auxiliary Space: O(1), no extra space required so it is a constant.Example 2: Java // Java program to check if String contains only Alphabets // Using ASCII Values // Main class class GFG { // Method 1 // To check String for only Alphabets public static boolean isStringOnlyAlphabet(String str) { // If there is no string if (str == null || str.equals("")) { return false; } // Iterating as now we encounter string // using length() method for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); // Checking for any other aphabetic character // present in string if ((!(ch >= 'A' && ch <= 'Z')) && (!(ch >= 'a' && ch <= 'z'))) { // Then string is not only alphabetic return false; } } // If we reach here, it means // string is only aphabetic return true; } // Method 2 // Main method public static void main(String[] args) { // Checking for True case System.out.println("Test Case 1:"); String str1 = "GeeksforGeeks"; System.out.println("Input: " + str1); System.out.println("Output: " + isStringOnlyAlphabet(str1)); // Checking for String with numeric characters System.out.println("\nTest Case 2:"); String str2 = "Geeks4Geeks"; System.out.println("Input: " + str2); System.out.println("Output: " + isStringOnlyAlphabet(str2)); // Checking for null String System.out.println("\nTest Case 3:"); String str3 = null; System.out.println("Input: " + str3); System.out.println("Output: " + isStringOnlyAlphabet(str3)); // Checking for empty String System.out.println("\nTest Case 4:"); String str4 = ""; System.out.println("Input: " + str4); System.out.println("Output: " + isStringOnlyAlphabet(str4)); } } OutputTest Case 1: Input: GeeksforGeeks Output: true Test Case 2: Input: Geeks4Geeks Output: false Test Case 3: Input: null Output: false Test Case 4: Input: Output: falseComplexity Analysis: Time Complexity: O(N), where N is the length of the string.Auxiliary Space: O(1), no extra space required so it is a constant.Related Articles: Check if a string contains only alphabets in Java using Lambda expressionCheck if a string contains only alphabets in Java using Regex Comment More infoAdvertise with us C code_r Follow Improve Article Tags : Java Java-Strings ASCII Java-String-Programs Practice Tags : JavaJava-Strings Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like