83

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
lugeno
  • 1,055
  • 3
  • 10
  • 18

8 Answers8

161

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();
Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
  • 7
    I would like to avoid importing external libraries if not strictly necessary. And yes: I want à to be considered as non alphanumeric. – lugeno Nov 23 '11 at 21:19
  • a *side note:* unrelated but just to remind ```StringUtils.isAlphaNumeric("abc");``` returns ```true``` if the str contains only alphabets. – Brooklyn99 Jan 25 '21 at 02:50
29

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);
loscuropresagio
  • 1,922
  • 15
  • 26
6

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

Alternatively, you can use regex.

Mechkov
  • 4,294
  • 1
  • 17
  • 25
4

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.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
2

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

erip
  • 16,374
  • 11
  • 66
  • 121
Shaun
  • 2,446
  • 19
  • 33
1

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

nike4613
  • 39
  • 5
0

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;
}
Oguz
  • 1,867
  • 1
  • 17
  • 24
0

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