有没有一种方法可以评估 string.contains() 方法内的多个字符串?

发布于 2024-12-11 23:31:22 字数 196 浏览 0 评论 0原文

if (description.ToUpper().Contains("BOUGHT") || description.ToUpper().Contains("PURCHASE"))

上面的代码是我所拥有的,我想知道对于相同的条件是否有更长的字符串列表,我将如何在不使代码太长的情况下做到这一点。也许是 lambda 表达式?

if (description.ToUpper().Contains("BOUGHT") || description.ToUpper().Contains("PURCHASE"))

The code above is what I have and I wondered if I had a longer list of strings for the same condition, how I would do it without making the code too long. Maybe a lambda expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

望笑 2024-12-18 23:31:22

不,没有内置功能。不过自己写也不难:

string[] needles = new string[]{"BOUGHT", "PURCHASE"};
string haystack = description.ToUpperInvariant();
bool found = needles.Any(needle=> haystack.Contains(needle));

我只将hackstack转换为upper一次,以提高性能。

或者,您可以使用 IndexOf(needle, StringComparison.OrdinalIgnoreCase)>=0

string[] needles = new string[]{"BOUGHT", "PURCHASE"};
string haystack = description;
bool found = needles.Any(needle=> haystack.IndexOf(needle, StringComparison.OrdinalIgnoreCase)>=0);

您不应在此处使用 ToUpper(),因为它使用当前区域性。在某些计算机上使用当前区域性可能会导致意外问题,例如,使用土耳其区域性时,i 不会大写为 I

两侧的 ToUpperInvariant() 可能仍然存在一些微妙的问题,并且不区分大小写的比较可能会返回不同的结果,但只有当您的干草堆和针中都有不寻常的字符时,这才有意义。

No, there is no built in function. But it's not hard to write it yourself:

string[] needles = new string[]{"BOUGHT", "PURCHASE"};
string haystack = description.ToUpperInvariant();
bool found = needles.Any(needle=> haystack.Contains(needle));

I only convert hackstack to upper once to improve performance.

Alternatively you could use IndexOf(needle, StringComparison.OrdinalIgnoreCase)>=0:

string[] needles = new string[]{"BOUGHT", "PURCHASE"};
string haystack = description;
bool found = needles.Any(needle=> haystack.IndexOf(needle, StringComparison.OrdinalIgnoreCase)>=0);

You should not use ToUpper() here, since that uses the current culture. Using the current culture can lead to unexpected problems on some computers, for example i does not uppercase to I when using the Turkish culture.

There might still some subtle problems remaining where ToUpperInvariant() on both sides and a case insensitive comparison might return different results, but that's only relevant if you have unusual characters in both your haystack and needles.

半寸时光 2024-12-18 23:31:22

您可以将代码修改为如下所示:

var words = new[] { "BOUGHT", "PURCHASE" };
var desc = description.ToUpper();
if(words.Any(w => description.Contains(w)) {
    // something matched
}

You can rework the code to something like this:

var words = new[] { "BOUGHT", "PURCHASE" };
var desc = description.ToUpper();
if(words.Any(w => description.Contains(w)) {
    // something matched
}
仙女山的月亮 2024-12-18 23:31:22
if (someCollectionOfStrings.Any(string => originalString.Contains(string))
{
    //stuff
}
if (someCollectionOfStrings.Any(string => originalString.Contains(string))
{
    //stuff
}
亚希 2024-12-18 23:31:22

使用正则表达式:

  if (Regex.IsMatch(description, "purchase|bought", RegexOptions.IgnoreCase)) {
     // ...
  }

Use a regular expression:

  if (Regex.IsMatch(description, "purchase|bought", RegexOptions.IgnoreCase)) {
     // ...
  }
扬花落满肩 2024-12-18 23:31:22
Regex.IsMatch(input, string.Join("|", strings));

如果字符串包含正则表达式控制字符,您可能必须对它们进行转义。

Regex.IsMatch(input, string.Join("|", strings));

You might have to escape the strings if they contain Regex control characters.

小梨窩很甜 2024-12-18 23:31:22
public static bool ContainsOneOfManyIgnoreCase(this string str, params string [] items)
    {
      return items.Any(x => str.IndexOf(x, StringComparison.CurrentCultureIgnoreCase) != -1);
    }
public static bool ContainsOneOfManyIgnoreCase(this string str, params string [] items)
    {
      return items.Any(x => str.IndexOf(x, StringComparison.CurrentCultureIgnoreCase) != -1);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文