开发者

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)
{
    Re开发者_运维问答gex rg = new Regex("[^a-zA-Z0-9]");

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


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


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


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.


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;
        }


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;
    }


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


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 ^


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.


Char static methods can be used too

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜