Java,确保字符串只包含字母数字、空格和破折号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/749742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java, Make sure a String contains only alphanumeric, spaces and dashes
提问by Lancelot
In Java, I need to make sure a String only contains alphanumeric, spaceand dashcharacters.
在 Java 中,我需要确保 String 只包含alphanumeric,space和dash字符。
I found the class org.apache.commons.lang.StringUtilsand the almost adequate method isAlphanumericSpace(String)... but I also need to include dashes.
我找到了类org.apache.commons.lang.StringUtils和几乎足够的方法isAlphanumericSpace(String)……但我还需要包含破折号。
What is the best way to do this? I don't want to use Regular Expressions.
做这个的最好方式是什么?我不想使用正则表达式。
回答by Skip Head
You could use:
你可以使用:
StringUtils.isAlphanumericSpace(string.replace('-', ' '));
回答by Varkhan
Hum... just program it yourself using String.chatAt(int), it's pretty easy...
嗯...只需使用String.chatAt( int)自己编程,这很容易...
Iterate through all charin the string using a position index, then compare it using the fact that ASCII characters 0 to 9, a to z and A to Z use consecutive codes, so you only need to check that character x numerically verifies one of the conditions:
使用位置索引遍历字符串中的所有字符,然后使用 ASCII 字符 0 到 9、a 到 z 和 A 到 Z 使用连续代码的事实进行比较,因此您只需要检查字符 x 以数字方式验证其中一个条件:
- between '0' and '9'
- between 'a' and 'z'
- between 'A and 'Z'
- a space ' '
- a hyphen '-'
- 在“0”和“9”之间
- 'a' 和 'z' 之间
- 在“A”和“Z”之间
- 空间 ' '
- 连字符“-”
Here is a basic code sample (using CharSequence, which lets you pass a String but also a StringBuilder as arg):
这是一个基本的代码示例(使用 CharSequence,它允许您传递一个 String 和一个 StringBuilder 作为 arg):
public boolean isValidChar(CharSequence seq) {
int len = seq.length();
for(int i=0;i<len;i++) {
char c = seq.charAt(i);
// Test for all positive cases
if('0'<=c && c<='9') continue;
if('a'<=c && c<='z') continue;
if('A'<=c && c<='Z') continue;
if(c==' ') continue;
if(c=='-') continue;
// ... insert more positive character tests here
// If we get here, we had an invalid char, fail right away
return false;
}
// All seen chars were valid, succeed
return true;
}
回答by araqnid
Just iterate through the string, using the character-class methods in java.lang.Character to test whether each character is acceptable or not. Which is presumably all that the StringUtils methods do, and regular expressions are just a way of driving a generalised engine to do much the same.
只需遍历字符串,使用 java.lang.Character 中的字符类方法来测试每个字符是否可以接受。这大概是 StringUtils 方法所做的全部,而正则表达式只是驱动通用引擎执行相同操作的一种方式。
回答by MStodd
You have 1 of 2 options: 1. Compose a list of chars that CAN be in the string, then loop over the string checking to make sure each character IS in the list. 2. Compose a list of chars that CANNOT be in the string, then loop over the string checking to make sure each character IS NOT in the list.
您有 2 个选项中的 1 个: 1. 编写一个可以在字符串中的字符列表,然后循环检查字符串以确保每个字符都在列表中。2. 编写一个不能在字符串中的字符列表,然后循环检查字符串以确保每个字符都不在列表中。
Choose whatever option is quicker to compose the list.
选择任何可以更快地编写列表的选项。
回答by r3flss ExlUtr
Definitely use a regex expression. There's no point in writing your own system when a very comprehensive system in place for this exact task. If you need to learn about or brush up on regex then check out this website, it's great: http://regexr.com
绝对使用正则表达式。当有一个非常全面的系统来完成这个确切的任务时,编写自己的系统是没有意义的。如果您需要了解或复习正则表达式,请查看此网站,它很棒:http: //regexr.com
I would challenge yourself on this one.
我会在这个问题上挑战自己。

