Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

    var list=alist.Contains("somestring")

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

like image 965
zsharp Avatar asked Feb 17 '10 00:02

zsharp


People also ask

How to check if a list contains a particular string in Python?

Python3. The any() function is used to check the existence of an element in the list. it's like- if any element in the string matches the input element, print that the element is present in the list, else, print that the element is not present in the list.

How to find partial string match in Python?

Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.

How do I check if a list contains a string?

if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));

How do you check if a string contains a substring from a list Java?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.


1 Answers

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.

like image 187
Reed Copsey Avatar answered Oct 16 '22 23:10

Reed Copsey