【发布时间】:2013-05-02 13:52:47
【问题描述】:
所以我的代码是这样的
string order = "Im sending you big apples x100";
string[] fruits = { "apples", "big apples", "oranges" };
string[] vegetables = { "tomatoes", "carrots", "cucumber" };
string[] words = order.Split();
if (fruits.Any(w => words.Contains(w)))
{
//do things here
}
if (vegetables.Any(w => words.Contains(w)))
{
//do things here
}
如果可能的话,我希望能够根据订单字符串找到它到底是什么,现在在我的情况下,当字符串数组按顺序有 2 个单词时,此代码不起作用,当我的字符串数组时我该怎么做有2个字。我只想找到它是否有“大苹果”,我知道我只能做“苹果”,但我想在订单字符串中找到序列词。
【问题讨论】:
-
当您将
order拆分为space分隔符上的块时,您消除了在order中查找诸如“大苹果”之类的短语的能力。如果您正在寻找 Regex 的替代方法,您可以保持order字符串不变,并在其中搜索子字符串的出现。 msdn.microsoft.com/en-us/library/…
标签: c# string string-matching string-search