iProgrammer
iProgrammer

Reputation: 3107

check alphanumeric characters in string in c#

I have used the following code but it is returning false though it should return true

string check,zipcode;
zipcode="10001 New York, NY";
check=isalphanumeric(zipcode)

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex("[^a-zA-Z0-9]");

    //if has non AlpahNumeric char, return false, else return true.
    return rg.IsMatch(strToCheck) == true ? false : true;
}

Upvotes: 30

Views: 80779

Answers (9)

zzfima
zzfima

Reputation: 1565

Char static methods can be used too

bool IsAlphaNumeric(char charToCheck) => char.IsLetter(charToCheck) || char.IsDigit(charToCheck);

Upvotes: 0

Douglas Gaskell
Douglas Gaskell

Reputation: 10040

If you want a non-regex ASCII A-z 0-9 check, you cannot use char.IsLetterOrDigit() as that includes other Unicode characters, and is unreliable/unstable with unicode scalar values.

What you can do is check the character code ranges.

  • 48 -> 57 are numerics
  • 65 -> 90 are capital letters
  • 97 -> 122 are lower case letters

The following is a bit more verbose, but it's for ease of understanding rather than for code golf.

    public static bool IsAsciiAlphaNumeric(this string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return false;
        }

        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] < 48) // Numeric are 48 -> 57
            {
                return false;
            }

            if (str[i] > 57 && str[i] < 65) // Capitals are 65 -> 90
            {
                return false;
            }

            if (str[i] > 90 && str[i] < 97) // Lowers are 97 -> 122
            {
                return false;
            }

            if (str[i] > 122)
            {
                return false;
            }
        }

        return true;
    }

Upvotes: 7

James Heacock
James Heacock

Reputation: 61

I needed a method to see if the string contains any Alpha Numeric, without using Regex...

  public static bool ContainsAlphaNumeric(string strToCheck)
        {
            foreach(char c in strToCheck)
            {
                if (char.IsLetterOrDigit(c))
                {
                    return true;
                }
            }
            return false;
        }

Upvotes: 6

DeFKnoL
DeFKnoL

Reputation: 1

Back in my perl days, I would have used this regular expression:

\w+

which means one or more word character. A word character is basically a-zA-Z0-9 and basically does not care about punctuation or spaces. So if you just want to make sure that there is someting of value in the string, this is what I have used in C#:

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"\w+");
    return rg.IsMatch(strToCheck);
}

Thanks to Chopikadze for the basic structure.

I do think that this one would be faster since instead of checking through the entire string, it would stop at the first instance of a word character and return a true.

Upvotes: 0

hedi
hedi

Reputation: 1

tring check,zipcode;
zipcode="10001 New York, NY";
check=isalphanumeric(zipcode)

    public static Boolean isAlphaNumeric(string strToCheck)
            {
                Regex rg = new Regex("[^a-zA-Z0-9]");

                //if has non AlpahNumeric char, return false, else return true.
                return rg.IsMatch(strToCheck) == true ? false : true;
            }

this code return always false, because the symbole ^ means that's this string doesn't contains any alphanumeric caractere, you need to delete the this ^

Upvotes: 0

chopikadze
chopikadze

Reputation: 4239

Try this one:

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"^[a-zA-Z0-9\s,]*$");
    return rg.IsMatch(strToCheck);
}

It's more undestandable, if you specify in regex, what your string SHOULD contain, and not what it MUST NOT.

In the example above:

  • ^ - means start of the string
  • []* - could contain any number of characters between brackets
  • a-zA-Z0-9 - any alphanumeric characters
  • \s - any space characters (space/tab/etc.)
  • , - commas
  • $ - end of the string

Upvotes: 65

Yair Levi
Yair Levi

Reputation: 1357

    public static bool IsAlphaNumeric(string strToCheck)
    {
        return strToCheck.All(char.IsLetterOrDigit);
    }

Upvotes: 46

Brian Webster
Brian Webster

Reputation: 30865

10001 New York, NY contains a comma and spaces -- not alphanumeric

You need to adjust your expression to allow commas and spaces.

Also, you will probably want to rename the function so that it is clear to other developers that it is more of a validator than an isAlphaNumeric() function.

Upvotes: 6

rerun
rerun

Reputation: 25505

When the ^ is in the [ ] it means everything but these characters.

Upvotes: 2

Related Questions