检查字符串

时间:2013-11-01 10:27:47

标签: c# string char contains

我正在寻找一个快速的方法 - 我的问题的解决方案来检查一个字符串,如果这包含一个最小的字符。 IF String包含与字母表中任何一个字符相同的字符,然后返回true,否则返回false。

public bool checkString(String s)
{
  return true || false;
}

例如:

"1232133432454355467" return false 
"134324239846c" return true

6 个答案:

答案 0 :(得分:4)

尝试:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        var r = CheckString("112");
        Console.WriteLine(r); // false
        r = CheckString("112a");
        Console.WriteLine(r); // true
    }

    public static bool CheckString(String input)
    {
        return Regex.Match(input, @"[a-zA-Z]").Success;
        // or, as @Vlad L suggested
        //return Regex.IsMatch(input, @"[a-zA-Z]");
    }
}

如果要验证“All Letters”Unicode字符类,请改用此字符:

return Regex.IsMatch(input, @"\p{L}");

参考:Supported Unicode General Categories

答案 1 :(得分:2)

如果我正确理解了这个问题......如果字符串至少包含一个字母,则返回true。

    public bool checkString(String s)
    {
        return s.Any(x => Char.IsLetter(x));
    }

答案 2 :(得分:1)

尝试使用ToCharArray():

public bool checkString(String s)
{
    bool retValue =  s.ToCharArray()
                      .Any(c => ((int)c > 64 && (int)c < 91) || 
                                ((int)c > 96 && (int)c < 123));
    return retValue
}

答案 3 :(得分:1)

只是为了完成。

// Regex to check the value consists of letters
// with atleast 1 character
private static Regex reg = new Regex(@"[a-zA-Z]+");

public bool checkString(String s)
{
    return reg.Match(s).Success;
}

答案 4 :(得分:1)

这个怎么样?

if (Regex.IsMatch(yourString, "[a-zA-Z]"))
{
}

答案 5 :(得分:1)

static void Main(string[] args)
        {
            Console.WriteLine(checkString("137563475634c756"));
        }
        static  public bool checkString(String s)
        {
            return Regex.IsMatch(s, "[a-zA-Z]");
        }

它返回True。

相关问题