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

后端 未结 2 1234
天涯浪人
天涯浪人 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.Contains(), which checks for a specific element of the list.

提交回复
热议问题