【问题标题】:Check if a string contains only letters, digits and underscores检查字符串是否只包含字母、数字和下划线
【发布时间】:2015-12-14 10:05:23
【问题描述】:

我必须检查一个字符串是否只包含字母、数字和下划线。 这是我尝试的方法,但它不起作用:

for(int i = 0; i<=snameA.Length-1; i++)
{
    validA = validA && (char.IsLetterOrDigit(snameA[i])||snameA[i].Equals("_"));
}

【问题讨论】:

  • 尝试使用正则表达式检查此模式:([A-Za-z0-9\-_]+)
  • 我不知道。 validA 是一个布尔值,只有在满足条件时才必须设置为 true,但即使字符串具有特殊字符,它也设置为 true
  • “不起作用”意味着什么?错误?结果无效?
  • 注意:代码在此期间被修改了。
  • 我添加了“validA &&”,但还是不行。

标签: c# .net regex linq


【解决方案1】:

我喜欢 Linq 的这类问题:

bool validA = sname.All(c => Char.IsLetterOrDigit(c) || c.Equals('_'));

【讨论】:

    【解决方案2】:

    您每次都重新分配validA,而不检查其先前的值。现在,您始终可以获取上次执行的检查的值。

    你可以'和'结果:

    validA &= (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');
    

    这意味着您仍然运行所有字符,如果第一次检查失败,这可能无用。所以如果失败了最好直接退出:

    for(int i = 0; i<=snameA.Length-1; i++)
    {
        validA = (char.IsLetterOrDigit(snameA[i]) || snameA[i] == '_');
    
        if (!validA)
        { break; } // <-- see here
    }
    

    或使用 LINQ:

    validA = snameA.All(c => char.IsLetterOrDigit(c) || c == '_');
    

    【讨论】:

    • 我撤回了我的反对票。我猜使用正则表达式是一个选择问题,你的答案也是完全正确的。
    • 谢谢。我并不是说 OP 不应该使用正则表达式,但现在他知道他的代码出了什么问题。 :) 谢谢你的诚实。
    【解决方案3】:

    你可以使用正则表达式

    Regex regex1 = new Regex(@"^[a-zA-Z0-9_]+$");
    
    if(regex1.IsMatch(snameA))
    {
    
    }
    

    【讨论】:

    • Regex 将它自己的实例作为参数!
    • 顺便说一句,a-zA-Z0-9_\w 相同
    • @Thomas:不,有一个细微的差别:a-zA-Z0-9_ 只接受 English 字母,而 \w 接受 any 字母,例如Переменная(俄语变量)。你说得对:问题中的当前实现(通过char.IsLetterOrDigit)对应于正则表达式中的\w*
    【解决方案4】:

    我会使用正则表达式

    string pattern = @"^[a-zA-Z0-9\_]+$";
    Regex regex = new Regex(pattern);
    
    // Compare a string against the regular expression
    return regex.IsMatch(stringToTest);
    

    【讨论】:

      【解决方案5】:

      您可以尝试匹配正则表达式。 “字母、数字和下划线”有一个内置类型,即“\w”。

      Regex rgx = new Regex(@"\w*");
      rgs.IsMatch(yourString);
      

      如果您需要 1 个或更多,则使用“\w+”。

      更多信息在这里:Regex.IsMatch

      【讨论】:

      • 如果一个字符串包含个字母、数字和下划线 =>你的检查字符串是否包含任何个字母,数字和下划线
      【解决方案6】:

      首先,字母有点模糊:你的意思是a..zA..Z字符或字母可以属于任何字母,例如а..яА..Я(俄语,西里尔字母)。根据您的当前实现,您需要第二个选项

      使用循环的典型解决方案是检查直到第一个反例:

        Boolean validA = true; // true - no counter examples so far
      
        // Why for? foreach is much readble here
        foreach(Char ch in sname) 
          // "!= '_'" is more readable than "Equals"; and wants no boxing
          if (!char.IsLetterOrDigit(ch) && ! (ch != '_')) { 
            Boolean validA = false; // counter example (i.e. non-letter/digit symbol found)
      
            break; // <- do not forget this: there's no use to check other characters
          }
      

      但是您可以使用 Linq 来简化代码:

        validA = sname.All(ch => Char.IsLetterOrDigit(ch) || ch == '_');
      

      正则表达式

        validA = Regex.IsMatch(sname, @"^\w*$");
      

      【讨论】:

        猜你喜欢
        • 2011-07-11
        • 2017-11-02
        • 2012-08-20
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        相关资源
        最近更新 更多