【问题标题】:Find a keyword taken from a string and look for it in a list of string从字符串中查找关键字并在字符串列表中查找
【发布时间】:2012-12-11 15:59:13
【问题描述】:

我有一个 list 包含 4 个项目的字符串:

Orange Lemon Pepper Tomato

另外,我有一个String str,里面有一句话:

Today, I ate a tomato and an orange.

1) 我如何检查str 是否有来自list 的一些关键字?不考虑大小写字母,基本上捕获任何匹配的东西?

我试过了,但它不起作用,因为它会查找相同的单词。 list.Contains(str)

还有Dim result As String() = list.FindAll(str, Function(s) s.ToLower().Contains(str)),但也没有用。

2) 如果tomato 中的tomatoesstr 中的tomatoes,我怎样才能检测到tomato 部分并丢弃es 部分?

有什么建议或想法吗?

【问题讨论】:

    标签: c# regex vb.net


    【解决方案1】:
    var list = new string[] { "Orange", "Lemon", "Pepper", "Tomato" };
    var str = "Today, I ate a tomato and an orange.";
    

    使用 LINQ 和正则表达式,您可以检查字符串是否包含任何关键字:

    list.Any(keyword => Regex.IsMatch(str, Regex.Escape(keyword), RegexOptions.IgnoreCase));
    

    或者获取匹配的关键字:

    var matched = list.Where(keyword =>
                    Regex.IsMatch(str, Regex.Escape(keyword), RegexOptions.IgnoreCase));
    // "Orange", "Tomato"
    

    顺便说一句,这将匹配tomatoesfootomato。如果你需要匹配词的开头,那么搜索模式应该稍微改变一下:@"(^|\s)" + keyword

    【讨论】:

    • 不要忘记对您的关键字调用 Regex.Escape。如果您有一个带有特殊正则表达式字符的关键字,您将不会得到运行时异常......所以 Regex.IsMatch(str, Regex.Escape(keyword)....)。
    【解决方案2】:

    如果区分大小写不是问题,您可以这样做:

    List<string> test = new List<string>();
    test.Add("Lemon");
    test.Add("Orange");
    test.Add("Pepper");
    test.Add("Tomato");
    
    string str = "Today, I ate a tomato and an orange.";
    
    foreach (string s in test)
    {
          // Or use StringComparison.OrdinalIgnoreCase when cultures are of no issue.
          if (str.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) > -1)
          {
              Console.WriteLine("Sentence contains word: " + s);
          }
    }
    
    Console.Read();
    

    【讨论】:

    • First: IndexOf > -1, 0 可以是字符串中的第一个位置。第二:使用 str.IndexOf(s, StringComparison.IgnoreCase) 代替。
    • 感谢编辑,不知道CurrentCultureIgnoreCase :)
    【解决方案3】:
    Regex reg = new Regex("(Orange|lemon|pepper|Tomato)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    MatchCollection mc = reg.Matches("Today, I ate tomatoes and an orange.");
    foreach (Match mt in mc)
    {
        Debug.WriteLine(mt.Groups[0].Value);
    }
    

    【讨论】:

      【解决方案4】:
          Dim str As String = "Today, I ate a tomato and an orange"
          Dim sWords As String = "Orange Lemon Pepper Tomato"
          Dim sWordArray() As String = sWords.Split(" ")
      
          For Each sWord In sWordArray
      
              If str.ToLower.Contains(sWord.ToLower) Then
                  Console.WriteLine(sWord)
              End If
      
          Next sWord
      

      【讨论】:

      【解决方案5】:

      使用list.Contains(str),您正在检查list 是否包含整个字符串。你需要做的是检查strlist 中是否有字,如下所示:

      foreach(var s in list)
      {
           if(str.ToLower().Contains(s.ToLower()))
           {
                //do your code here
           }
      }
      

      这将遍历您的列表,并检查您的 str 以查看它是否在其中。它还将解决您的问题 2。由于 tomatotomatoes 的一部分,因此它将通过该检查。 ToLower() 部分使所有内容都小写,通常在您想忽略大小写时使用。

      【讨论】:

      【解决方案6】:
      Private Function stringContainsOneOfMany(ByVal haystack As String, ByVal needles As String()) As Boolean
          For Each needle In needles
              If haystack.ToLower.Contains(needle.ToLower) Then
                  Return True
              End If
          Next
          Return False
      End Function
      

      使用:

          Dim keywords As New List(Of String) From {
              "Orange", "Lemon", "Pepper", "Tomato"}
          Dim str As String = "Today, I ate a tomato and an orange"
          If stringContainsOneOfMany(str, keywords.ToArray) Then
              'do something
          End If
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多