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

本文关键字:CurrentCultureIgnoreCase StringComparison 数组 检查 另一个 字符串 | 更新日期: 2023-09-27 18:21:56

请帮助了解如何在以下语句中使用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;
}

例如
if dummyAccount="resturant business"
则Account="业务";

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

希望这能奏效。

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;
}

您可以通过每个尝试

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

试试这个

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;
}
相关文章:
  • 没有找到相关文章