Given a java.lang.String instance, I want to verify that it doesn't contain any unicode characters that are not ASCII alphanumerics. eg The string should be limited to [A-Za-z0-9.]. What I'm doing now is something very inefficient:
import org.apache.commons.lang.CharUtils;
String s = ...;
char[] ch = s.toCharArray();
for( int i=0; i<ch.length; i++)
{
if( ! CharUtils.isAsciiAlphanumeric( ch[ i ] )
throw new InvalidInput( ch[i] + " is invalid" );
}
Is there a better way to solve this ?
您可以使用
input.matches("[A-Za-z0-9.]+")
Yes, there's a better way to solve that. You already have written the pattern, so why don't you use a regular expression to validate it? Instead of throwing an exception that includes the invalid character you could just aswell use a generic error message saying something along the lines of "input contains invalid characters (valid characters are az and 0-9)".
Try this:
private boolean isBasicLatin(String input)
{
for (char c : input.toCharArray())
{
if (!UnicodeBlock.BASIC_LATIN.equals(UnicodeBlock.of(c)))
{
return false;
}
}
return true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.