There are a couple of ways to do this, depending on what exactly you want to do.
If (as the question says) you want to remove all characters that are not "A-Z, 0-9, and symbols that can be typed using the shift key and a number, such as ! and @", the best way is to construct a regular expression pattern that matches the characters you don't want to remove, and use the String.matches(String)
and String.replaceAll(String, String)
methods:
private static final String NON_NORMAL_CHARACTERS_PATTERN = "\\W|[^!@#\\$%\\^&\\*\\(\\)]";
public static boolean hasSymbols(String string) {
return string.matches(NON_NORMAL_CHARACTERS_PATTERN);
}
public static String removeSymbols(String string) {
return string.replaceAll(NON_NORMAL_CHARACTERS_PATTERN, "");
}
The pattern above called NON_NORMAL_CHARACTERS_PATTERN
matches non-word characters with \W
, and everything except the Shift+[0-9] characters with [^!@#\$%\^&\*\(\)]
.
If what you want is to remove all characters that are not in the 127 character ASCII set, you can exploit the fact that for these characters, Character.getNumericValue(char)
will always be less than or equal to 127:
public static boolean isNonASCII(char character) {
return Character.getNumericValue(character) > 127;
}
public static boolean hasNonASCII(String string) {
for (char currentChar : string.toCharArray()) {
if (isNonASCII(currentChar)) {
return false;
}
}
return true;
}
public static String removeNonASCII(String string) {
StringBuilder stringBuilder = new StringBuilder();
for (char currentChar : string.toCharArray()) {
if (!isNonASCII(currentChar)) {
stringBuilder.append(currentChar);
}
}
return stringBuilder.toString();
}