How do I check a string to make sure it contains numbers, letters, or space only?
如何检查字符串以确保它只包含数字、字母或空格?
7 个解决方案
#1
45
The easiest way it to use a regular expression:
使用正则表达式最简单的方法是:
Regular Expression for alphanumeric and underscores
字母数字和下划线的正则表达式
Using regular expressions in .net:
在。net中使用正则表达式:
http://www.regular-expressions.info/dotnet.html
http://www.regular-expressions.info/dotnet.html
MSDN正则表达式
Regex.IsMatch
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
if(regexItem.IsMatch(YOUR_STRING)){..}
#2
38
Simple:
简单:
function HasSpecialChars(string yourString)
{
return yourString.Any( ch => ! Char.IsLetterOrDigit( ch ) )
}
#3
12
string s = @"$KUH% I*$)OFNlkfn$";
var withoutSpecial = new string(s.Where(c => Char.IsLetterOrDigit(c)
|| Char.IsWhiteSpace(c)).ToArray());
if (s != withoutSpecial)
{
Console.WriteLine("String contains special chars");
}
#4
5
Try this way.
尝试这种方式。
public static bool hasSpecialChar(string input)
{
string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,";
foreach (var item in specialChar)
{
if (input.Contains(item)) return true;
}
return false;
}
#5
4
String test_string = "tesintg#$234524@#";
if (System.Text.RegularExpressions.Regex.IsMatch(test_string, "^[a-zA-Z0-9\x20]+$"))
{
// Good-to-go
}
An example can be found here: http://ideone.com/B1HxA
可以在这里找到一个示例:http://ideone.com/B1HxA
#6
3
If the list of acceptable characters is pretty small, you can use a regular expression like this:
如果可以接受的字符列表很小,可以使用这样的正则表达式:
Regex.IsMatch(items, "[a-z0-9 ]+", RegexOptions.IgnoreCase);
The regular expression used here looks for any character from a-z and 0-9 including a space (what's inside the square brackets []), that there is one or more of these characters (the + sign--you can use a * for 0 or more). The final option tells the regex parser to ignore case.
这里使用的正则表达式查找来自a-z和0-9的任何字符,包括空格(方括号内的内容[]),这些字符中有一个或多个(+符号——可以使用*表示0或更多)。最后一个选项告诉regex解析器忽略大小写。
This will fail on anything that is not a letter, number, or space. To add more characters to the blessed list, add it inside the square brackets.
这将在任何不是字母、数字或空间的东西上失败。要向受祝福的列表添加更多字符,请将其添加到方括号内。
#7
2
Use the regular Expression below in to validate a string to make sure it contains numbers, letters, or space only:
使用下面的正则表达式验证字符串,以确保它只包含数字、字母或空格:
[a-zA-Z0-9 ]
#1
45
The easiest way it to use a regular expression:
使用正则表达式最简单的方法是:
Regular Expression for alphanumeric and underscores
字母数字和下划线的正则表达式
Using regular expressions in .net:
在。net中使用正则表达式:
http://www.regular-expressions.info/dotnet.html
http://www.regular-expressions.info/dotnet.html
MSDN正则表达式
Regex.IsMatch
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
if(regexItem.IsMatch(YOUR_STRING)){..}
#2
38
Simple:
简单:
function HasSpecialChars(string yourString)
{
return yourString.Any( ch => ! Char.IsLetterOrDigit( ch ) )
}
#3
12
string s = @"$KUH% I*$)OFNlkfn$";
var withoutSpecial = new string(s.Where(c => Char.IsLetterOrDigit(c)
|| Char.IsWhiteSpace(c)).ToArray());
if (s != withoutSpecial)
{
Console.WriteLine("String contains special chars");
}
#4
5
Try this way.
尝试这种方式。
public static bool hasSpecialChar(string input)
{
string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,";
foreach (var item in specialChar)
{
if (input.Contains(item)) return true;
}
return false;
}
#5
4
String test_string = "tesintg#$234524@#";
if (System.Text.RegularExpressions.Regex.IsMatch(test_string, "^[a-zA-Z0-9\x20]+$"))
{
// Good-to-go
}
An example can be found here: http://ideone.com/B1HxA
可以在这里找到一个示例:http://ideone.com/B1HxA
#6
3
If the list of acceptable characters is pretty small, you can use a regular expression like this:
如果可以接受的字符列表很小,可以使用这样的正则表达式:
Regex.IsMatch(items, "[a-z0-9 ]+", RegexOptions.IgnoreCase);
The regular expression used here looks for any character from a-z and 0-9 including a space (what's inside the square brackets []), that there is one or more of these characters (the + sign--you can use a * for 0 or more). The final option tells the regex parser to ignore case.
这里使用的正则表达式查找来自a-z和0-9的任何字符,包括空格(方括号内的内容[]),这些字符中有一个或多个(+符号——可以使用*表示0或更多)。最后一个选项告诉regex解析器忽略大小写。
This will fail on anything that is not a letter, number, or space. To add more characters to the blessed list, add it inside the square brackets.
这将在任何不是字母、数字或空间的东西上失败。要向受祝福的列表添加更多字符,请将其添加到方括号内。
#7
2
Use the regular Expression below in to validate a string to make sure it contains numbers, letters, or space only:
使用下面的正则表达式验证字符串,以确保它只包含数字、字母或空格:
[a-zA-Z0-9 ]