【问题标题】:How to check if string contains at least one alphabetical letter (a-z,A-Z) C# [duplicate]如何检查字符串是否包含至少一个字母(a-z,A-Z)C# [重复]
【发布时间】:2018-03-16 01:07:48
【问题描述】:

有人能告诉我如何检查一个字符串是否至少包含一个字母吗? 我试过了:

if (StringName.Text.Contains(Char.IsLetter()))
{
 //Do Something
}

但它不起作用。

【问题讨论】:

    标签: c# regex contains


    【解决方案1】:

    您可以使用 LINQ:

    if (StringName.Text.Any(Char.IsLetter))
    {
        // Do something
    }
    

    【讨论】:

    • 很奇怪,在你之前没有人想出任何接近这个答案的东西......也许another universe......
    【解决方案2】:

    试试 Linq。如果您接受任何 Unicode 字母,例如俄语ъ

    if (StringName.Text.Any(c => char.IsLetter(c))) 
    {
        // Do Something
    }
    

    如果你只想要a..zA..Z

    if (StringName.Text.Any(c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) 
    {
        // Do Something
    }
    

    最后,如果你坚持正则表达式

    if (Regex.IsMatch(StringName.Text, @"\p{L}")) 
    {
       // Do Something 
    }  
    

    或者(第二个选项)a..z 以及 A..Z 仅限字母

    if (Regex.IsMatch(StringName.Text, @"[a-zA-Z]")) 
    {
       // Do Something 
    }  
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2013-11-22
      • 2010-11-02
      • 2012-10-04
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      相关资源
      最近更新 更多