如何在检查另一个字符串中的数组项时使用StringComparison.CurrentCultureIgnoreCase

时间:2014-09-16 06:39:38

标签: c# string

请帮助找出如何在以下语句中使用StringComparison.CurrentCultureIgnoreCase。我在这里检查数组元素是否存在于字符串 dummyAccount 中。

一切都很好我只想使用StringComparison.CurrentCultureIgnoreCase。

private string getAccount(string dummyAccount)
{
    //e.g dummyAccount="resturant business";
    string Account = string.Empty;

    if ((new string[] { "abc", "Xyz","MD" }).Any(dummyAccount.Contains))
    {
        Account = "Unknown account";
    }
    else if ((new string[] { "shop", "hotel", "Resturant","Business"}).Any(dummyAccount.Contains))
    {
        Account = "Business";
    }
    else if ((new string[] { "school", "college" }).Any(dummyAccount.Contains))
    {
        Account = "University";
    }
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    return dummyAccount;
}

e.g
  如果dummyAccount =“resturant business”;
然后账户=“业务”;

3 个答案:

答案 0 :(得分:1)

希望这有效。

private string getAccount(string dummyAccount)
{
    //e.g dummyAccount="resturant business";
    string Account = string.Empty;

    if ((new string[] { "abc", "Xyz", "MD" }).Any(a => dummyAccount.IndexOf(a, StringComparison.InvariantCultureIgnoreCase)>=0))
    {
        Account = "Unknown account";
    }
    else if ((new string[] { "shop", "hotel", "Resturant", "Business" }).Any(a => dummyAccount.IndexOf(a, StringComparison.InvariantCultureIgnoreCase) >= 0))
    {
        Account = "Business";
    }
    else if ((new string[] { "school", "college" }).Any(a => dummyAccount.IndexOf(a, StringComparison.InvariantCultureIgnoreCase) >= 0))
    {
        Account = "University";
    }
    return dummyAccount;
}

答案 1 :(得分:0)

您可以尝试这种方法

if ((new string[] { "abc", "Xyz", "MD" }).Any(x => dummyAccount.IndexOf(x, StringComparison.CurrentCultureIgnoreCase) >= 0))
{
    Account = "Unknown account";
}

答案 2 :(得分:-1)

试试这个

private string getAccount(string dummyAccount)
{
    //e.g dummyAccount="resturant business";
    string Account = string.Empty;

    if ((new string[] { "abc", "Xyz","MD" }).Any(s => dummyAccount.ToLower().Contains(s.ToLower())))
    {
        Account = "Unknown account";
    }
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    --------------------------------------------------------------
    return dummyAccount;
}