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();
Answer from Fabian Barney on Stack OverflowUsing 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();
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);
java - Check if String contains only letters - Stack Overflow
Java- How to find non-alphabetical letters in a string? (Quick way) - Stack Overflow
Is there any easy way to check if a string contains non-alphabet character?
How do I check if a string contains a letter?
What do you want? Speed or simplicity? For speed, go for a loop based approach. For simplicity, go for a one liner RegEx based approach.
Speed
public boolean isAlpha(String name) {
char[] chars = name.toCharArray();
for (char c : chars) {
if(!Character.isLetter(c)) {
return false;
}
}
return true;
}
Simplicity
public boolean isAlpha(String name) {
return name.matches("[a-zA-Z]+");
}
Java 8 lambda expressions. Both fast and simple.
boolean allLetters = someString.chars().allMatch(Character::isLetter);
A for loop, you can use the Character class to determine if each character is a Letter (or other type). See: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isAlphabetic(int)
You should probably use a regular expression:
Pattern patt = Pattern.compile("[^A-Za-z]");
Matcher mat = patt.matcher("avc@dgh");
boolean found = mat.find();
System.out.println(found ? mat.start() : -1);
Just exclude using ^ the alphabetical letters:
[^a-zA-Z]+
See demo
If you want to check that text does not contain any letter [A-Za-z], try this:
if (!text.matches("[A-Za-z]+")) {
System.out.println("no letters");
}
If you want to check that text contains only numbers and the given special characters, try this:
String regex = "[0-9\\-/@#$%^&_+=()]+";
if (text.matches(regex)) {
System.out.println("no letters");
}
Note that the - must be escpade by a \ which itself must be escaped.
If i understnad correctly, the characters you want to remove are of a rather limited set. Why not just check for these? Unicode has a whole bunch of non-letter characters, but in your case, the non-letter characters encountered will probably be a small subset of what exists.
Sounds like a job for regular expressions, if you ask me. Remove everything that's not a word character, digit or whitespace, and you've probably got it. Or create an array containing all characters you want filtered out (which in that case should be few and known).
You could implement a Charset that contains only the characters you want. You can then provide a CharsetDecoder to decode the text and strip out the characters you want to skip.