Alphanumeric character

A character or letter which is either an alphabet or a number is called an Alphanumeric character.
Simply saying, characters which are from among a-z or A-Z or 0-9 are alphanumeric. All characters other than these are called non-alphanumeric characters. All special characters come under this category.
Examples of such characters are !, @, #, $, %, ^, &, *, (, ), _, +, =, space etc.

Given a String, you may want to check the following:

  1. String contains any non-alphanumeric(or special) characters?
  2. String is completely alphanumeric?
  3. String contains a space

After reading this article, you will be able to do all of the above and that too in many ways.

Method 1 : Using java.lang.Character class

Convert the string to check into a character array. Iterate over the characters and check every character using isLetterOrDigit() method of java.lang.Character class.
This method takes a character as argument and returns true if the character is an alphabet or a number. Below are sample runs of a program with different string values.

static void checkAlphaNumeric(String stringToTest) { 
  // convert string to corresponding characters 
  char[] chars = stringToTest.toCharArray(); 
  boolean isNonAlphaNumericCharacter = false; 
  //iterate over characters 
  for (int i = 0; i < chars.length; i++) { 
    char c = chars[i]; 
    //check if the character is alphanumeric 
    if (!Character.isLetterOrDigit(c)) { 
      //if non-alphanumeric, set a flag 
      isNonAlphaNumericCharacter = true; 
    } 
  } 
  if(isNonAlphaNumericCharacter){ 
    System.out.println("String contains " + 
                   "non-alphanumeric characters"); 
  } else { 
    System.out.println("String contains " +
                   "only alphanumeric characters"); 
  } 
} 

/** 
* Test method 
* 
* @param args 
*/ 
public static void main(String[] args) { 
  checkAlphaNumeric("codippa is nice"); 
  checkAlphaNumeric("codippaisnice"); 
  checkAlphaNumeric("www.codippa.com"); 
}

Output

String contains non-alphanumeric characters
String contains only alphanumeric characters
String contains non-alphanumeric characters

The first string contains a space which is a non-alphanumeric, second string contains no special characters and third string contains . as a special character.

Method 2 : Using Apache Commons Lang Library

Lang library from Apache has a static method isAlphaNumeric() in class org.apache.commons.lang<version> package.
This method takes a String as an argument, checks whether it contains any character other than alphabets or numbers and returns true if all the characters are alphanumeric, false otherwise.

public static void main(String[] args) { 
  System.out.println(StringUtils.isAlphanumeric("codippa is nice")); 
  System.out.println(StringUtils.isAlphanumeric("codippaisnice")); 
  System.out.println(StringUtils.isAlphanumeric("www.codippa.com")); 
}

Output

false
true
false

Program is run on the same input values as above example and produces the same output.

Maven dependency of Apache Commons Lang3 library is given below:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

Method 3 : Matching pattern in String class

java.lang.String class has a matches() method which takes a String as an argument.
This string argument is a regular expression which tests for a pattern consisting of only alphabets and numbers.
The string which calls matches() is tested against this regular expression and the method returns true if the string matches the regular expression, false otherwise.

public static void main(String[] args) { 
  String stringToTest = "codippa is nice"; 
  boolean hasNonAlphaNumeric = s.matches("^.*[^a-zA-Z0-9].*$"); 
  System.out.println(hasNonAlphaNumeric); 
  stringToTest = "codippaisnice"; 
  hasNonAlphaNumeric = s.matches("^.*[^a-zA-Z0-9].*$"); 
  System.out.println(hasNonAlphaNumeric); 
  stringToTest = "www.codippa.com"; 
  hasNonAlphaNumeric = s.matches("^.*[^a-zA-Z0-9].*$"); 
  System.out.println(hasNonAlphaNumeric); 
}

Output

true
false
true

Only the second string is purely alphanumeric, hence it prints false.
The key point to note in the above program is the regular expression ^.*[^a-zA-Z0-9].*$ which tests for any character other than a-z, A-Z and 0-9.
Thus, if it finds any character other than these, it returns true(means non-alphanumeric character).

Method 4 : Using java Pattern matcher

java.util.regex package has classes for matching patterns in a String.
The main class of this package is java.util.regex.Pattern which has a compile() method.
compile() takes a pattern to match in any string as argument.

Pattern class has another method called matches() which takes the source string to be matched against a regular expression as an argument and returns a java.util.regex.Matcher object.

Matcher is another class in the same package.
This class has a find() method which does the main work.
It matches the string against the supplied pattern and returns true if the string contains a sequence of characters matching the expression, false otherwise.

public static void main(String[] args) { 
  Pattern p = Pattern.compile("^.*[^a-zA-Z0-9].*$"); 
  String stringToTest = "codippa is nice"; 
  boolean hasNonAlphaNumeric = p.matcher(s).find(); 
  System.out.println(hasNonAlphaNumeric); 
  stringToTest = "codippaisnice"; 
  hasNonAlphaNumeric = p.matcher(s).find(); 
  System.out.println(hasNonAlphaNumeric); 
  stringToTest = "www.codippa.com"; 
  hasNonAlphaNumeric = p.matcher(s).find(); 
  System.out.println(hasNonAlphaNumeric); 
}

Output

true
false
true

Regular expression ^.*[^a-zA-Z0-9].*$ tests for any character other than a-z, A-Z and 0-9. Thus if it finds any character other than these, it returns true(means non-alphanumeric character).

Hope one of the above methods helped to solve a problem. If yes, the post will be considered a success. Keep visiting for more !!!