问题
if i create a text filter in the following manner
class AlphaTextFilter extends TextFilter {
public char convert(char c, int status) {
if (!validate(c))
return 0;
return c;
}
public boolean validate(char c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
}
will it work if the code is run on a device with some language other than english...? how can i make a localized....?
回答1:
That depends on what your definition of "work" is. If by "work" you mean accepts only [a-zA-Z], then yes.
If you mean would it meet the expectations of international users who expect to be using alphabetic, syllabic or ideographic characters (or "letters" in more common usage), then no. Most platforms now offer some mechanism for identifying the unicode character class, and most newer regex engines support matching based on unicode character classes. In your case, \p{letter} would appear to be the most appropriate character class.
回答2:
What about using something like CharacterUtilities.isLetter() to check if it's a letter?
回答3:
Yes, I believe so.
A char
is a char
no matter what locale you're in. It's all Unicode anyway, so there shouldn't be any sort of localisation issues.
来源:https://stackoverflow.com/questions/2016817/text-filter-localization