Elegant way to count alphanumeric chars in a string?

Viewed 5662

I am looking for an elegant way, preferably a short linq expression, to count how many alphanumeric chars a given string contains.

The 'boring' way I do it now is this:

int num = 0;
for (int i = 0; i < password.Length; i++)
{
    if (!char.IsLetterOrDigit(password, i))
    {
        num++;
    }
}
if (num < MinRequiredNonAlphanumericCharacters)
    return false;

This is rather short already, but I am sure with some linq magic this can be done in an even shorter, equally understandable expression, right?

4 Answers
Related