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

后端 未结 8 1266
一向
一向 2020-11-27 02:44

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.

8条回答
  •  醉话见心
    2020-11-27 03:09

    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.

提交回复
热议问题