Skip to content
Advertisement

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

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.

Advertisement

Answer

Using Apache Commons Lang:

JavaScript

Alternativly iterate over String’s characters and check with:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement