Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查c中字符串中的字母数字字符#_C# - Fatal编程技术网

C# 检查c中字符串中的字母数字字符#

C# 检查c中字符串中的字母数字字符#,c#,C#,我使用了下面的代码,但是它返回false,尽管它应该返回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 cha

我使用了下面的代码,但是它返回false,尽管它应该返回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;
}

10001纽约州纽约市
包含逗号和空格,而不是字母数字

您需要调整表达式以允许逗号和空格


此外,您可能希望重命名该函数,以便其他开发人员清楚地知道它更像是一个验证器,而不是一个isAlphaNumeric()函数

当“^”在[]中时,它表示除这些字符以外的所有字符

试试这个:

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"^[a-zA-Z0-9\s,]*$");
    return rg.IsMatch(strToCheck);
}
如果在正则表达式中指定字符串应该包含的内容,而不是不能包含的内容,则更难理解

在上述示例中:

  • ^-表示字符串的开始
  • []*-括号之间可以包含任意数量的字符
  • a-zA-Z0-9-任何字母数字字符
  • \s-任何空格字符(空格/制表符/等)
  • ,-逗号
  • $-字符串的结尾

此代码返回值始终为false,因为符号表示此字符串不包含任何字母数字字符,您需要删除此^

在我的perl时代,我会使用此正则表达式:

\w+

表示一个或多个单词字符。单词字符基本上是A-zA-Z0-9,基本上不关心标点或空格。如果你只是想确保字符串中有一些值,这就是我在C#中使用的:

感谢Chopikadze的基本结构


我确实认为这个方法会更快,因为它不会检查整个字符串,而是在单词字符的第一个实例处停止并返回true。

我需要一个方法来查看字符串是否包含字母数字,而不使用正则表达式

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

如果需要非正则ASCII
a-z 0-9
检查,则不能使用
char.isleterOrdigit()
,因为这包括其他Unicode字符。

您可以做的是检查字符代码范围

  • 48->57是数字
  • 65->90是大写字母
  • 97->122是小写字母
下面的内容有点冗长,但这是为了便于理解,而不是为了编写代码

    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;
    }
公共静态bool IsAsciiAlphaNumeric(此字符串str)
{
if(string.IsNullOrEmpty(str))
{
返回false;
}
对于(int i=0;i57
{
返回false;
}
如果(str[i]>57&&str[i]<65)//大写字母为65->90
{
返回false;
}
如果(str[i]>90&&str[i]<97)//降低值为97->122
{
返回false;
}
如果(str[i]>122)
{
返回false;
}
}
返回true;
}

您需要将其放宽,以便允许使用空格和逗号。YU或示例字符串中有“,”字符可能会有所帮助。请参阅本文中的解决方案-您的代码有5个错误:1。)^不包括括号中的以下字符。2.)不允许使用逗号和空格,3.)缺少作为第一个字符的“^”,以及作为最后一个字符的$。4.)您还需要允许逗号,它不是字母数字,因此您的函数名称错误。“==true?false:true”完全是胡说八道。要反转布尔值,请使用“not”运算符“!”。如果正则表达式是正确的,则无需反转结果。我不明白为什么人们给你10票赞成这样一个可耻的代码?10张反对票就足够了。逗号不是字母数字字符@Elmue我仍然觉得这个答案更一般、更有用。我并不是在寻找一个能给我字母、数字和一些一次性标点符号的解决方案。我在谷歌上搜索字母和数字的解决方案,这个答案最适合我。我并没有说这个答案是错的。我的评论是指上面约翰·桑德斯的评论。我的错误@elmue你的答案是错误的。^不是唯一的错误。即使删除它,它仍然是错误的。此正则表达式中存在多个错误。你认为这样的答案会赢得声誉吗?这个答案也是错误的。您的正则表达式不允许空格,它只搜索给定字符串中的任何单词。它不会检查整个字符串是否为字母数字。所以你的答案有多处错误!这是本页上唯一正确的答案。请注意,这个正则表达式还允许一个完全为空的字符串。所以我会用a+替换*。逗号不是字母数字,因此函数的名称具有误导性。如果你想要一个较短的版本,你可以写:@“^[\w\s,]+$”我添加了一个答案,避免使用正则表达式,只接受
a-z a-z 0-0
,这与大多数使用
char
函数的答案不同。这就像检查字符代码范围一样简单。我刚刚运行了一个测试,并且
zipcode.Any(c=>char.isleterordigit(c))
大约占正则表达式答案时间的1/100,并且支持非ascii字符。(至少使用示例邮政编码输入。这允许使用非A-Z字母,如汉字等。)
public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"\w+");
    return rg.IsMatch(strToCheck);
}
  public static bool ContainsAlphaNumeric(string strToCheck)
        {
            foreach(char c in strToCheck)
            {
                if (char.IsLetterOrDigit(c))
                {
                    return true;
                }
            }
            return false;
        }
    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;
    }