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.
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();
StringUtils.isAlphaNumeric("abc"); returns true if the str contains only alphabets.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);
hasSpecialCharacters(String) with his own definition of "special".String.matches(...) in Java checks if the regex matches the whole string.boolean hasNonAlpha = ! … ^[A-Za-z0-9 ]*$You have to go through each character in the String and check Character.isDigit(char); or Character.isLetter(char);
Alternatively, you can use regex.
Character.isLetterOrDigit(char) method -- (since 1.5 the corresponding Character.isLetterOrDigit(int) one)à is considered a letter by Character)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.
à is considered a letter by Character)string.matches("^\\W*$"); should do what you want, but it does not include whitespace. string.matches("^(?:\\W|\\s)*$"); does match whitespace as well.
à is considered a word character)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;
}
à is considered a letter by Character)I had to check a string contains at least 1 letter and 1 digit - my solution below
/**
* <p>Checks if the String contains both letters and digits.</p>
*
* <p>{@code null} will return {@code false}.
* An empty String str.isEmpty() will return {@code false}.</p>
*
* @param str the String to check, may be null
* @return {@code true} if only contains letters and digits,
* and is non-null or not empty
*/
public static boolean isAlphaAndNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*$");
boolean containsSpecialChars = !pattern.matcher(str).matches();
if (containsSpecialChars) {
return false;
}
boolean containsLetter = false;
boolean containsDigit = false;
char[] chars = str.toCharArray();
int i = 0;
while (i < chars.length) {
if (Character.isLetter(chars[i])) {
containsLetter = true;
}
if (Character.isDigit(chars[i])) {
containsDigit = true;
}
i++;
if (containsDigit && containsLetter) {
return true;
}
}
return false;
}
àcan be considered alphabetic, punctuation marks also according [Merian Webster)