67

Possible Duplicate:
C# Regex: Checking for “a-z” and “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.

Community
  • 1
  • 1
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • 2
    I vote to reopen the question since it isn't strictly related to a regex. – Omar May 06 '15 at 22:09
  • 1
    I'd also argue that this isn't a duplicate. At least not of a duplicate of _C# Regex: Checking for “a-z” and “A-Z”_ since this question is asking wether a string contains any char a-zA-Z and the other question is asking of the string solely contains of a-zA-Z... – BTC Sep 02 '15 at 09:51

6 Answers6

113

What about:

//true if it doesn't contain letters
bool result = hello.Any(x => !char.IsLetter(x));
Omar
  • 16,329
  • 10
  • 48
  • 66
  • 15
    Note: this checks if there are any characters that are not a letter, it does not check if a string has letters in it. Simple fix: bool result = !hello.Any(x => char.IsLetter(x)); – Qwerty01 Jul 22 '14 at 15:17
  • 9
    I'm glad there's a LINQ answer here, so I don't have to use Regex. – Taylor K. Aug 13 '14 at 13:59
  • 2
    Shouldn't `!hello.Any(char.IsLetter);` also work? –  Mar 25 '16 at 16:48
  • björn: That will return true if no character is a letter, which is not what was asked. In a somewhat related manner, the question was specifically for A-Z, a-z, althought that might not be what was really wanted. – perh Mar 30 '16 at 18:06
  • 1
    If the string has a new line "\r" or "\n" then it will return true – Dman Feb 14 '17 at 09:34
  • 1
    Works great for me! – Kbdavis07 Jun 30 '18 at 20:45
  • 1
    char.IsLetter will match any unicode characters too. not just a-z in English. – CSS Nov 11 '20 at 10:39
85

Replace your for loop by this :

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

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

John Smith
  • 7,243
  • 6
  • 49
  • 61
Ta Duy Anh
  • 1,478
  • 9
  • 16
27

You could use RegEx:

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

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

hello.All(Char.IsLetter);

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

Char.IsLetter(character);
Kirby
  • 3,649
  • 3
  • 26
  • 28
7

You can look for regular expression

Regex.IsMatch(str, @"^[a-zA-Z]+$");
Sanja Melnichuk
  • 3,465
  • 3
  • 25
  • 46
4

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.

perh
  • 1,668
  • 11
  • 14
3

Use regular expression no need to convert it to char array

if(Regex.IsMatch("yourString",".*?[a-zA-Z].*?"))
{
errorCounter++;
}
Anirudha
  • 32,393
  • 7
  • 68
  • 89