简体   繁体   English

如何按顺序检查字符串中的单词

[英]How to check a string for words in sequence

So my code looks like this 所以我的代码看起来像这样

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
}

I want to be able to find depending on the order string what exactly is it if its possible, now in my case when the string array has 2 words in sequence this code doesnt work, how can i do it when my string array has 2 words. 我希望能够根据顺序字符串找到可能的情况到底是什么,现在在我的情况下,当字符串数组按顺序包含2个单词时,此代码不起作用,当我的字符串数组具有2个单词时,我该怎么办。 I want to find only if it has "big apples" i know i could do it only "apples" but i want to find sequence words in the order string. 我想仅在它具有“大苹果”的情况下才能找到,我知道我只能在“苹果”中做到,但我想在顺序字符串中找到顺序词。

If you're searching for a substring you don't need to split the order string into individual words. 如果要搜索子字符串,则无需将order字符串拆分为单个单词。 You can use the String.Contains method (in this case, replace words.Contains with order.Contains ). 您可以使用String.Contains方法(在这种情况下,更换words.Containsorder.Contains )。

if (fruits.Any(w => order.Contains(w)))
{
  //do things here
}
if (vegetables.Any(w => order.Contains(w)))
{
  //do things here
}

If your search can be case-insenitive, you can use the IndexOf method. 如果您的搜索可能不区分大小写,则可以使用IndexOf方法。

if(fruits.Any(w => order.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0))
{
   //do things here
}
if (vegetables.Any(w => order.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0))
{
   //do things here
}

As per comment, this will match substrings of words (eg apple will match apples ). 根据评论,这将匹配单词的子字符串(例如apple将匹配apples )。 If it has to be whole words only then you can use the regex in Govind Kumar's answer (which looks to have been copied from this answer ). 如果只需要完整的单词,则可以在Govind Kumar的答案中使用regex (看起来是从该答案中复制过来的 )。 You'd use it like this: 您可以这样使用它:

var fruitWords = fruits.Select(w => @"\b" + Regex.Escape(w) + @"\b");   
var fPattern = new Regex("(" + string.Join(")|(", fruitWords) + ")");
var fruitMatch = fPattern.IsMatch(order);

var vegWords = fruits.Select(w => @"\b" + Regex.Escape(w) + @"\b");   
var vPattern = new Regex("(" + string.Join(")|(", vegWords) + ")");
var vegMatch = vPattern.IsMatch(order);

if(fruitMatch)
{   
   //fruit matched
}
if(vegMatch)
{
   //veg matched
}
var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b");

var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
var q = pattern.IsMatch(myText);

This will let you loop through each type of fruit and then check the value. 这将使您遍历每种水果,然后检查其值。 Since you already have an approach which uses Lambda syntax, I thought I'd share a non lambda version! 既然您已经有使用Lambda语法的方法,我想我应该共享一个非lambda版本!

    foreach (var fruit in fruits)
    {
        if (order.contains(fruit))
        {
           //do things here
        }
    }
    foreach (var veg in vegetables)
    { 
        if (order.contains(veg))
        {
           //do things here
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM