简体   繁体   English

Java 检查字符串是否只包含英文键盘字母

[英]Java check if string only contains english keyboard letters

I want to disallow users from using any special characters in their name.我想禁止用户在他们的名字中使用任何特殊字符。 They should be able to use the whole english keyboard, so他们应该能够使用整个英文键盘,所以

a-z, 0-9, [], (), &, ", %, $, ^, °, #, *, +, ~, §, ., ,, -, ', =, }{

and so on.等等。 So they should be allowed to use every "normal" english character which you can type with your keyboard.所以他们应该被允许使用你可以用键盘输入的每一个“正常”的英文字符。

How can I check that?我该如何检查?

Use regex to match name with English alphabets.使用正则表达式将名称与英文字母匹配。

Solution 1:解决方案1:

if(name.matches("[a-zA-Z]+")) {
  // Accept name
}
else {
  // Ask to enter again
}

Solution 2:解决方案2:

while(!name.matches("[a-zA-Z]+")) {
  // Ask to enter again
}
// Accept name

We can do like:我们可以这样做:

String str = "My string";
System.out.println(str.matches("^[a-zA-Z][a-zA-Z\\s]+$"));//true

str = "My string1";
System.out.println(str.matches("^[a-zA-Z][a-zA-Z\\s]+$"));//false

You can use a regular expression for this.您可以为此使用正则表达式

Since you have lots of characters that have special meaning in a regular expression, I recommend putting them in a separate string and quoting them:由于您在正则表达式中有许多具有特殊含义的字符,因此我建议将它们放在单独的字符串中并引用它们:

String specialCharacters = "-[]()&...";
Pattern allowedCharactersPattern = Pattern.compile("[A-Za-z0-9" + Pattern.quote(specialCharacters) +  "]*");

boolean containsOnlyAllowedCharacters(String str) {
    return allowedCharactersPattern.matcher(str).matches();
}

As for how to obtain the string of special characters in the first place, there is no way to list all the characters that can be typed with the user's current keyboard layout.至于如何首先获取特殊字符的字符串,目前还没有办法列出用户当前的键盘布局可以输入的所有字符。 In fact, since there are ways to type any Unicode character at all such a list would be useless anyway.事实上,因为有办法输入任何 Unicode 字符,所以这样的列表无论如何都是无用的。

I find the requirement to be quite strange, in that I can't see the rationale behind accepting § but not, say, å , and I have not checked the list of characters you want to accept in any detail.我觉得这个要求很奇怪,因为我看不出接受§但不接受å背后的理由,而且我没有详细检查你想要接受的字符列表。

But, it seems to me that what you're asking is to accept any character whose codepoint value is less than 0x0080, with the oddball exception of § (0x00A7).但是,在我看来,你要问的是接受任何代码点值小于 0x0080 的字符,除了§ (0x00A7) 的奇怪例外。 So I'd code it to make that check explicitly, and not get involved with regular expressions.所以我会对其进行编码以明确地进行检查,而不是涉及正则表达式。 I assume you want to exclude control characters, even though they can be typed on an English keyboard.我假设您想排除控制字符,即使它们可以在英文键盘上输入。

Pseudocode:伪代码:

 for each character ch in string
      if ch < 0x0020 || (ch >= 0x007f && ch != `§')
           then it's not allowed

Your requirements are oddly-stated though, in that you want to disallow "special characters" but allow `.@#$%6&*()_+' for example?不过,您的要求很奇怪,因为您想禁止“特殊字符”但允许 `.@#$%6&*()_+' 例如? What's your definition of "special character"?你对“特殊字符”的定义是什么?

For arbitrary definition of 'allowable characters' I'd use a bitset.对于“允许字符”的任意定义,我会使用 bitset。

static BitSet valid = new Bitset();
static {
    valid.set('A', 'Z'+1);
    valid.set('a', 'z'+1);
    valid.set('0', '9'+1);
    valid.set('.');
    valid.set('_');
      ...etc...
}

then然后

for (int j=0; j<str.length(); j++)
    if (!valid.get(str.charAt(j))
        ...illegal...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM