[英]Verifying that a string contains only letters in C# (if, else function or Regex)
[英]Verifying that a string contains only letters in C#
我有一个输入字符串,我想验证它是否包含:
为了澄清,我在代码中有 3 种不同的情况,每种情况都需要不同的验证。 在 C# 中实现这一目标的最简单方法是什么?
"只有字母:
Regex.IsMatch(input, @"^[a-zA-Z]+$");
只有字母和数字:
Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");
只有字母、数字和下划线:
Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
bool result = input.All(Char.IsLetter);
bool result = input.All(Char.IsLetterOrDigit);
bool result = input.All(c=>Char.IsLetterOrDigit(c) || c=='_');
仅字母:
Regex.IsMatch(theString, @"^[\p{L}]+$");
字母和数字:
Regex.IsMatch(theString, @"^[\p{L}\p{N}]+$");
字母、数字和下划线:
Regex.IsMatch(theString, @"^[\w]+$");
请注意,这些模式还匹配国际字符(与使用az
构造相反)。
对于那些不想使用 Regex 并且使用 .NET 2.0 框架(又名没有 LINQ)的人:
只有字母:
public static bool IsAllLetters(string s)
{
foreach (char c in s)
{
if (!Char.IsLetter(c))
return false;
}
return true;
}
只有数字:
public static bool IsAllDigits(string s)
{
foreach (char c in s)
{
if (!Char.IsDigit(c))
return false;
}
return true;
}
只有数字或字母:
public static bool IsAllLettersOrDigits(string s)
{
foreach (char c in s)
{
if (!Char.IsLetterOrDigit(c))
return false;
}
return true;
}
仅数字或字母或下划线:
public static bool IsAllLettersOrDigitsOrUnderscores(string s)
{
foreach (char c in s)
{
if (!Char.IsLetterOrDigit(c) && c != '_')
return false;
}
return true;
}
我认为使用正则表达式是一个很好的例子:
public bool IsAlpha(string input)
{
return Regex.IsMatch(input, "^[a-zA-Z]+$");
}
public bool IsAlphaNumeric(string input)
{
return Regex.IsMatch(input, "^[a-zA-Z0-9]+$");
}
public bool IsAlphaNumericWithUnderscore(string input)
{
return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$");
}
您可以循环字符串的字符并使用 Char 方法IsLetter 进行检查,但您也可以使用 String 方法IndexOfAny来搜索其他不应该出现在字符串中的字符。
遍历字符串字符并使用名为“IsLetter”和“IsDigit”的“Char”函数。
如果您需要更具体的东西 - 使用 Regex 类。
如果您是新手,那么您可以参考我的代码。我所做的是进行检查,以便我只能获得字母和空格! 您可以在第二个 if 语句之后重复 for 循环以再次验证字符串
bool check = false;
Console.WriteLine("Please Enter the Name");
name=Console.ReadLine();
for (int i = 0; i < name.Length; i++)
{
if (name[i]>='a' && name[i]<='z' || name[i]==' ')
{
check = true;
}
else
{
check = false;
break;
}
}
if (check==false)
{
Console.WriteLine("Enter Valid Value");
name = Console.ReadLine();
}
最近,我在此页面的帮助下对检查字符串中的字母的函数进行了性能改进。
我发现使用正则表达式的解决方案比使用 Char.IsLetterOrDigit 检查的解决方案慢 30 倍。
我们不确定这些字母或数字是否包括在内,我们只需要拉丁字符,因此基于 Char.IsLetterOrDigit 函数的反编译版本实现了我们的函数。
这是我们的解决方案:
internal static bool CheckAllowedChars(char uc)
{
switch (uc)
{
case '-':
case '.':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
}
用法是这样的:
if( logicalId.All(c => CheckAllowedChars(c)))
{ // Do your stuff here.. }
请找到验证char是字母、数字还是空格的方法,否则附加下划线(可根据需要随意修改)
public String CleanStringToLettersNumbers(String data)
{
var result = String.Empty;
foreach (var item in data)
{
var c = '_';
if ((int)item >= 97 && (int)item <= 122 ||
(int)item >= 65 && (int)item <= 90 ||
(int)item >= 48 && (int)item <= 57 ||
(int)item == 32)
{
c = item;
}
result = result + c;
}
return result;
}
如果您想扫描字符串中的每个字符,请尝试此操作。 PS这是我第一次回答。
string myInput = "";
bool check = false;
for (int i = 0; i > -1; i++)
{
Console.WriteLine("Enter Letters Only");
myInput = Console.ReadLine();
for (int e = 0; e < myInput.Length; e++)
{
Console.WriteLine("Checking Character \"{0}\" ",myInput[e]);
if (Char.IsLetter(myInput[e])) //Letters only
{
check = true;
}
else if (myInput[e] == ' ') //Includes "SPACE" character
{
check = true;
}
else
{
check = false;
Console.WriteLine("wrong input. \"{0}\" is not a string", myInput[e]);
Console.WriteLine("pls try again");
break;
}
}
if (check == true) //Exits from input loop
{
break;
}
}
Console.WriteLine("Input Approved: \"{0}\"", myInput);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
我还没有看到使用模式匹配的解决方案:
public static bool ContainsOnlyLetters(this string input)
{
bool isValid = true;
for (int i = 0; isValid && i < input.Length; i++)
{
isValid &= input[i] is > 'A' and < 'Z' or > 'a' and < 'z';
}
return isValid;
}
或者如果你真的很讨厌可读的代码:
public static bool ContainsOnlyLetters(this string input)
{
bool isValid = true;
for (int i = 0; i < input.Length && (isValid &= input[i] is > 'A' and < 'Z' or > 'a' and < 'z'); i++);
return isValid;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.