如何检查字符串是否包含从a到z的任何字母?(复制)

时间:2020-12-19 01:34:02

Possible Duplicate:
C# Regex: Checking for “a-z” and “A-Z”

可能的副本:c# Regex:检查“a-z”和“a-z”

I could just use the code below:

我可以用下面的代码:

String hello = "Hello1";
Char[] convertedString = String.ToCharArray();
int errorCounter = 0;
for (int i = 0; i < CreateAccountPage_PasswordBox_Password.Password.Length; i++) {
    if (convertedString[i].Equals('a') || convertedString[i].Equals('A') .....
                            || convertedString[i].Equals('z') || convertedString[i].Equals('Z')) {
        errorCounter++;
    }
}
if(errorCounter > 0) {
    //do something
}

but I suppose it takes too much line for just a simple purpose, I believe there is a way which is much more simple, the way which I have not yet mastered.

但我认为,仅仅为了一个简单的目的,就需要写太多的句子,我相信有一种方法比这简单得多,那就是我还没有掌握的方法。

6 个解决方案

#1


40  

Replace your for loop by this :

将for循环替换为:

errorCounter = Regex.Matches(yourstring,@"[a-zA-Z]").Count;

Remember to use Regex class, you have to using System.Text.RegularExpressions; in your import

记住要使用Regex类,必须使用system . text . regular . arexpressions;在你的进口

#2


49  

What about:

是什么:

//true if it doesn't contain letters
bool result = hello.Any(x => !char.IsLetter(x));

#3


14  

You could use RegEx:

您可以使用正则表达式:

Regex.IsMatch(hello, @"^[a-zA-Z]+$");

If you don't like that, you can use LINQ:

如果你不喜欢,你可以用LINQ:

hello.All(Char.IsLetter);

Or, you can loop through the characters, and use isAlpha:

或者,你可以对字符进行循环,使用isAlpha:

Char.IsLetter(character);

#4


4  

You can look for regular expression

您可以查找正则表达式

Regex.IsMatch(str, @"^[a-zA-Z]+$");

#5


1  

For a minimal change:

最小的变化:

for(int i=0; i<str.Length; i++ )
   if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
      errorCount++;

You could use regular expressions, at least if speed is not an issue and you do not really need the actual exact count.

您可以使用正则表达式,至少如果速度不是问题,并且不需要实际的精确计数。

#6


0  

Use regular expression no need to convert it to char array

使用正则表达式无需将其转换为char数组。

if(Regex.IsMatch("yourString",".*?[a-zA-Z].*?"))
{
errorCounter++;
}

#1


40  

Replace your for loop by this :

将for循环替换为:

errorCounter = Regex.Matches(yourstring,@"[a-zA-Z]").Count;

Remember to use Regex class, you have to using System.Text.RegularExpressions; in your import

记住要使用Regex类,必须使用system . text . regular . arexpressions;在你的进口

#2


49  

What about:

是什么:

//true if it doesn't contain letters
bool result = hello.Any(x => !char.IsLetter(x));

#3


14  

You could use RegEx:

您可以使用正则表达式:

Regex.IsMatch(hello, @"^[a-zA-Z]+$");

If you don't like that, you can use LINQ:

如果你不喜欢,你可以用LINQ:

hello.All(Char.IsLetter);

Or, you can loop through the characters, and use isAlpha:

或者,你可以对字符进行循环,使用isAlpha:

Char.IsLetter(character);

#4


4  

You can look for regular expression

您可以查找正则表达式

Regex.IsMatch(str, @"^[a-zA-Z]+$");

#5


1  

For a minimal change:

最小的变化:

for(int i=0; i<str.Length; i++ )
   if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
      errorCount++;

You could use regular expressions, at least if speed is not an issue and you do not really need the actual exact count.

您可以使用正则表达式,至少如果速度不是问题,并且不需要实际的精确计数。

#6


0  

Use regular expression no need to convert it to char array

使用正则表达式无需将其转换为char数组。

if(Regex.IsMatch("yourString",".*?[a-zA-Z].*?"))
{
errorCounter++;
}