Java 8 – How to check whether given String contains Alphanumeric ?

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

1. Check String contains only Alphanumeric :

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

1.1 Using Java 8 Stream.allMatch() and Character.isLetterOrDigit(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.isLetterOrDigit(ch) method to check whether passed character is Alphanumeric 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 alphanumeric (true) or not (false)

CheckStringContainsOnlyAlphaNumericUsingJava8.java

package in.bench.resources.string.operation;

public class CheckStringContainsOnlyAlphaNumericUsingJava8 {

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

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


	/**
	 * This method is used to check whether passed String contains alphabets only
	 * using Java 8 Stream
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isAlphaNumeric(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.isLetterOrDigit(ch));
	}
}

Output:

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

1.2 Using for-loop and Character.isLetterOrDigit(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.isLetterOrDigit(ch) method to check whether passed character is Alphanumeric 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 alphanumeric (true) or not (false)

CheckStringContainsOnlyAlphaNumeric.java

package in.bench.resources.string.operation;

public class CheckStringContainsOnlyAlphaNumeric {

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

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


	/**
	 * This method is used to check whether passed String contains alphanumeric only
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isAlphaNumeric(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 alphanumeric
			if(!Character.isLetterOrDigit(ch)) {

				// return false
				return false;
			}
		}

		// otherwise, return true
		return true;
	}
}

Output:

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

References:

Happy Coding !!
Happy Learning !!

Java 8 – Find Largest number in an Arrays or List or Stream ?
Java 8 – How to check whether given String contains only Digits ?