var list=alist.Contains("somestring")
this matches whole string, how to see if any word in list has a substring matching "somestring"?
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.
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.
if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With