Sure, here is the translation: Java检查字符串是否只包含英文字母键盘字母。

huangapple go评论80阅读模式
英文:

Java check if string only contains english keyboard letters

问题

我想禁止用户在他们的名字中使用任何特殊字符。
他们应该能够使用整个英文键盘,所以

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

等等。所以他们应该被允许使用每个可以用键盘输入的“正常”英文字符。

我如何进行检查?

英文:

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?

答案1

得分: 2

我们可以这样做:

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
英文:

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

答案2

得分: 2

使用正则表达式(regex)来匹配只包含英文字母的姓名。

解决方案1:

if(name.matches("[a-zA-Z]+")) {
  // 接受姓名
}
else {
  // 要求重新输入
}

解决方案2:

while(!name.matches("[a-zA-Z]+")) {
  // 要求重新输入
}
// 接受姓名
英文:

Use regex to match name with English alphabets.

Solution 1:

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

Solution 2:

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

答案3

得分: 1

你可以使用正则表达式来实现这个。

由于你有许多在正则表达式中具有特殊意义的字符,我建议将它们放在一个单独的字符串中并加以引用:

String specialCharacters = "-[]()&..."; // 将特殊字符放入这个字符串中
Pattern allowedCharactersPattern = Pattern.compile("[A-Za-z0-9" + Pattern.quote(specialCharacters) + "]*");

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

至于如何获取特殊字符的字符串,首先,没有办法列出用户当前键盘布局可以键入的所有字符。实际上,由于有方法可以键入任何Unicode字符,这样的列表将毫无用处。

英文:

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.

答案4

得分: 0

我觉得这个要求相当奇怪,因为我无法理解为什么要接受 § 而不接受像是 å 这样的字符,而且我没有详细检查您想要接受的字符列表。

但是,我认为您所要求的是接受任何代码点值小于 0x0080 的字符,特殊的例外是 §(0x00A7)。所以我会编写代码来明确进行检查,而不涉及正则表达式。我假设您想要排除控制字符,尽管它们可以在英语键盘上输入。

伪代码:

对于字符串中的每个字符 ch
    如果 ch < 0x0020 || (ch >= 0x007f && ch != `&#167;`)
        则不允许

不过,您的要求表述得很奇怪,因为您想要禁止“特殊字符”,但却允许像 !@#$%6&amp;*()_+ 这样的字符。您对“特殊字符”的定义是什么?

对于任意定义的“允许字符”,我会使用位集(bitset)。

静态位集 valid = new Bitset();
静态 {
    valid.set('A', 'Z'+1);
    valid.set('a', 'z'+1);
    valid.set('0', '9'+1);
    valid.set('.');
    valid.set('_');
    ...等等...
}

然后

对于 (int j=0; j<str.length(); j++)
    如果 (!valid.get(str.charAt(j)))
        ...非法...
英文:

I find the requirement to be quite strange , in that I can't see the rationale behind accepting &#167; but not, say, &#229;, 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 &#167; (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 &lt; 0x0020 || (ch &gt;= 0x007f &amp;&amp; ch != `&#167;&#39;)
           then it&#39;s not allowed

Your requirements are oddly-stated though, in that you want to disallow "special characters" but allow `!@#$%6&*()_+' for example. What's your definition of "special character"?

For arbitrary definition of 'allowable characters' I'd use a bitset.

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

then

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

huangapple
  • 本文由 发表于 2020年8月17日 19:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63449997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定