【问题标题】:Search for specific text in list string and return whole line在列表字符串中搜索特定文本并返回整行
【发布时间】:2014-02-12 18:45:55
【问题描述】:

我有一个列表字符串:

List<string> Entries = new List<string>();
Entries.Add("The quick brown fox jumps over the lazy dog");
Entries.Add("Lorem ipsum dolor sit amet");
Entires.Add("Consetetur sadipscing elitr");

我可以在列表中搜索 "fox" 之类的内容并返回整行 ("The quick brown fox jumps over the lazy dog") 吗?

【问题讨论】:

  • 您可以为每个条目做一个foreach 并做contains "fox"?

标签: c# list search return


【解决方案1】:
Entries.Where(e=>e.Contains("fox")).FirstOrDefault()

【讨论】:

    【解决方案2】:

    第 1 步:您可以使用 foreach 循环遍历 Entries
    第 2 步: 在每个循环项上调用 Contains() 方法以检查必填字符串(fox)

    foreach (var item in Entries)
    {
        if (item.Contains("fox"))
            Console.WriteLine(item); //item found
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2018-05-05
      • 2018-10-15
      • 2015-02-25
      相关资源
      最近更新 更多