How to determine if a String has non-alphanumeric characters?

JavaStringCharacterAlphanumeric

Java Problem Overview


I need a method that can tell me if a String has non alphanumeric characters.

For example if the String is "abcdef?" or "abcdefà", the method must return true.

Java Solutions


Solution 1 - Java

Using Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

Alternativly iterate over String's characters and check with:

!Character.isLetterOrDigit(char)

You've still one problem left: Your example string "abcdefà" is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?!

So you may want to use regular expression instead:

String s = "abcdefà";
Pattern p = Pattern.compile("[^a-zA-Z0-9]");
boolean hasSpecialChar = p.matcher(s).find();

Solution 2 - Java

One approach is to do that using the String class itself. Let's say that your string is something like that:

String s = "some text";
boolean hasNonAlpha = s.matches("^.*[^a-zA-Z0-9 ].*$");

one other is to use an external library, such as Apache commons:

String s = "some text";
boolean hasNonAlpha = !StringUtils.isAlphanumeric(s);

Solution 3 - Java

You have to go through each character in the String and check Character.isDigit(char); or Character.isletter(char);

Alternatively, you can use regex.

Solution 4 - Java

Use this function to check if a string is alphanumeric:

public boolean isAlphanumeric(String str)
{
    char[] charArray = str.toCharArray();
    for(char c:charArray)
    {
        if (!Character.isLetterOrDigit(c))
            return false;
    }
    return true;
}

It saves having to import external libraries and the code can easily be modified should you later wish to perform different validation checks on strings.

Solution 5 - Java

If you can use the Apache Commons library, then Commons-Lang StringUtils has a method called isAlphanumeric() that does what you're looking for.

Solution 6 - Java

string.matches("^\\W*$"); should do what you want, but it does not include whitespace. string.matches("^(?:\\W|\\s)*$"); does match whitespace as well.

Solution 7 - Java

You can use isLetter(char c) static method of Character class in Java.lang .

public boolean isAlpha(String s) {
	char[] charArr = s.toCharArray();
	
	for(char c : charArr) {
		if(!Character.isLetter(c)) {
			return false;
		}
	}
	return true;
}

Solution 8 - Java

Though it won't work for numbers, you can check if the lowercase and uppercase values are same or not, For non-alphabetic characters they will be same, You should check for number before this for better usability

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlugenoView Question on Stackoverflow
Solution 1 - JavaFabian BarneyView Answer on Stackoverflow
Solution 2 - JavaloscuropresagioView Answer on Stackoverflow
Solution 3 - JavaMechkovView Answer on Stackoverflow
Solution 4 - JavaDan BrayView Answer on Stackoverflow
Solution 5 - JavaShaunView Answer on Stackoverflow
Solution 6 - Javanike4613View Answer on Stackoverflow
Solution 7 - JavaOguzView Answer on Stackoverflow
Solution 8 - JavaAbhishek ChoudharyView Answer on Stackoverflow