check alphanumeric characters in string in c#

后端 未结 8 1044
轻奢々
轻奢々 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:53

    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

提交回复
热议问题