Java 8 – How to check whether given String contains only Alphabets or Letters ?

In this article, we will discuss how to check whether the given String contains only Alphabets or Letters using Java 8 Stream

1. Check String contains only Alphabets or Letters :

  1. Using Java 8 Stream.allMatch() and Character.isLetter(ch) methods
  2. Using for-loop and Character.isLetter(ch) method

1.1 Using Java 8 Stream.allMatch() and Character.isLetter(ch) methods

  • First, check whether the given String is not null and not empty otherwise return false
  • Once after the given String passes above validation then
    • get Stream using CharSequence.chars() method
    • validate each characters iterated by passing to Character.isLetter(ch) method to check whether passed character is Letter/Alphabet only
  • Finally, invoke this method with different Strings like
    • null
    • only letters or alphabets
    • empty string
    • space
    • only digits
    • alphanumeric
    • alphabets with spaces in between
    • only special characters
    • Combo of alphabets and special characters
  • On the basis of boolean return-value from the method, print passed String contains only letters/alphabets (true) or not (false)

CheckStringOnlyAlphabetsUsingJava8.java

package in.bench.resources.string.operation;

public class CheckStringOnlyAlphabetsUsingJava8 {

	// main() method
	public static void main(String[] args) {

		// print to console
		System.out.println("1. Whether null is only alphabets :- " 
				+ isAlphabets(null));
		System.out.println("2. Whether \"Bench\" is only alphabets :- " 
				+ isAlphabets("Bench"));
		System.out.println("3. Whether empty string (\"\") is only alphabets :- " 
				+ isAlphabets(""));
		System.out.println("4. Whether empty space (\" \") is only alphabets :- " 
				+ isAlphabets(" "));
		System.out.println("5. Whether \"123\" is only alphabets :- " 
				+ isAlphabets("123"));
		System.out.println("6. Whether \"Bench123\" is only alphabets :- " 
				+ isAlphabets("Bench123"));
		System.out.println("7. Whether \"Bench_Resources\" is only alphabets :- " 
				+ isAlphabets("Bench_Resources"));
		System.out.println("8. Whether \"!@#$%^&*()_+\" is only alphabets :- " 
				+ isAlphabets("!@#$%^&*()_+"));
		System.out.println("9. Whether \"asdf\" is only alphabets :- " 
				+ isAlphabets("asdf"));
		System.out.println("10. Whether \"SJ Admin\" is only alphabets :- " 
				+ isAlphabets("SJ Admin"));
	}


	/**
	 * This method is used to check whether passed String contains alphabets only
	 * using Java 8 Stream
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isAlphabets(String str) {

		// check for null & empty
		if(str == null || str.isEmpty() || str.length() == 0) {

			// return false
			return false;
		}


		// return if its all match
		return str.chars().allMatch(ch -> Character.isLetter(ch));
	}
}

Output:

1. Whether null is only alphabets :- false
2. Whether "Bench" is only alphabets :- true
3. Whether empty string ("") is only alphabets :- false
4. Whether empty space (" ") is only alphabets :- false
5. Whether "123" is only alphabets :- false
6. Whether "Bench123" is only alphabets :- false
7. Whether "Bench_Resources" is only alphabets :- false
8. Whether "!@#$%^&*()_+" is only alphabets :- false
9. Whether "asdf" is only alphabets :- true
10. Whether "SJ Admin" is only alphabets :- false

1.2 Using for-loop and Character.isLetter(ch) method

  • First, check whether the given String is not null and not empty otherwise return false
  • Once after the given String passes above validation, iterate using for-loop starting from index 0 till length of the String
    • get character one-by-one using String.charAt(index) method
    • validate each characters iterated by passing to Character.isLetter(ch) method to check whether passed character is Letter/Alphabet only
  • Finally, invoke this method with different Strings like
    • null
    • only letters or alphabets
    • empty string
    • space
    • only digits
    • alphanumeric
    • alphabets with spaces in between
    • only special characters
    • Combo of alphabets and special characters
  • On the basis of boolean return-value from the method, print passed String contains only letters/alphabets (true) or not (false)

CheckStringContainsOnlyAlphabets.java

package in.bench.resources.string.operation;

public class CheckStringContainsOnlyAlphabets {

	// main() method
	public static void main(String[] args) {

		// print to console
		System.out.println("1. Whether null is only alphabets :- " 
				+ isAlphabets(null));
		System.out.println("2. Whether \"Bench\" is only alphabets :- " 
				+ isAlphabets("Bench"));
		System.out.println("3. Whether empty string (\"\") is only alphabets :- " 
				+ isAlphabets(""));
		System.out.println("4. Whether empty space (\" \") is only alphabets :- " 
				+ isAlphabets(" "));
		System.out.println("5. Whether \"123\" is only alphabets :- " 
				+ isAlphabets("123"));
		System.out.println("6. Whether \"Bench123\" is only alphabets :- " 
				+ isAlphabets("Bench123"));
		System.out.println("7. Whether \"Bench_Resources\" is only alphabets :- " 
				+ isAlphabets("Bench_Resources"));
		System.out.println("8. Whether \"!@#$%^&*()_+\" is only alphabets :- " 
				+ isAlphabets("!@#$%^&*()_+"));
		System.out.println("9. Whether \"asdf\" is only alphabets :- " 
				+ isAlphabets("asdf"));
		System.out.println("10. Whether \"SJ Admin\" is only alphabets :- " 
				+ isAlphabets("SJ Admin"));
	}


	/**
	 * This method is used to check whether passed String contains alphabets only
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isAlphabets(String str) {

		// check for null & empty
		if(str == null || str.isEmpty() || str.length() == 0) {

			// return false
			return false;
		}


		// iterate through String - reading one character at a time
		for(int index = 0; index < str.length(); index++) {


			// get one character at a time
			char ch = str.charAt(index);


			// check passed character is letter/alphabet
			if(!Character.isLetter(ch)) {

				// return false
				return false;
			}
		}

		// otherwise, return true
		return true;
	}
}

Output:

1. Whether null is only alphabets :- false
2. Whether "Bench" is only alphabets :- true
3. Whether empty string ("") is only alphabets :- false
4. Whether empty space (" ") is only alphabets :- false
5. Whether "123" is only alphabets :- false
6. Whether "Bench123" is only alphabets :- false
7. Whether "Bench_Resources" is only alphabets :- false
8. Whether "!@#$%^&*()_+" is only alphabets :- false
9. Whether "asdf" is only alphabets :- true
10. Whether "SJ Admin" is only alphabets :- false

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether given String contains only Digits ?
Java 8 – Count and print number of Vowels and Consonants in a String