check alphanumeric characters in string in c#

后端 未结 8 964
轻奢々
轻奢々 2020-12-15 05:04

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=isalpha         


        
相关标签:
8条回答
  • 2020-12-15 05:56

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

    0 讨论(0)
  • 2020-12-15 06:08

    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.

    0 讨论(0)
提交回复
热议问题