How to check if any words in a list contain a partial string?

后端 未结 2 1239
天涯浪人
天涯浪人 2021-02-13 15:10
    var list=alist.Contains(\"somestring\")

this matches whole string, how to see if any word in list has a substring matching \"somestring\"?

相关标签:
2条回答
  • 2021-02-13 15:44

    You can use the Enumerable.Any method:

    bool contained = alist.Any( l => l.Contains("somestring") );
    

    This is checking each element using String.Contains, which checks substrings. You previously were using ICollection<string>.Contains(), which checks for a specific element of the list.

    0 讨论(0)
  • 2021-02-13 15:54
    var hasPartialMatch = alist.Split(' ').ToList()
          .Any(x => x.Contains("somestring"));
    
    0 讨论(0)
提交回复
热议问题