

Check if the list contains an item containing a string and get that item
source link: https://www.codesd.com/item/check-if-the-list-contains-an-item-containing-a-string-and-get-that-item.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if the list contains an item containing a string and get that item
While searching for an answer to this question, I've run into similar ones utilizing LINQ but I haven't been able to fully understand them (and thus, implement them), as I'm not familiarized with it. What I would like to, basically, is this:
- Check if any element of a list contains a specific string.
- If it does, get that element.
I honestly don't know how I would go about doing that. What I can come up with is this (not working, of course):
if (myList.Contains(myString))
string element = myList.ElementAt(myList.IndexOf(myString));
I know WHY it does not work:
myList.Contains()
does not returntrue
, since it will check for if a whole element of the list matches the string I specified.myList.IndexOf()
will not find an occurrence, since, as it is the case again, it will check for an element matching the string.
Still, I have no clue how to solve this problem, but I figure I'll have to use LINQ as suggested in similar questions to mine. That being said, if that's the case here, I'd like for the answerer to explain to me the use of LINQ in their example (as I said, I haven't bothered with it in my time with C#). Thank you in advance guys (and gals?).
EDIT: I have come up with a solution; just loop through the list, check if current element contains the string and then set a string equal to the current element. I'm wondering, though, is there a more efficient way than this?
string myString = "bla";
string element = "";
for (int i = 0; i < myList.Count; i++)
{
if (myList[i].Contains(myString))
element = myList[i];
}
You should be able to use Linq here:
var matchingvalues = myList
.Where(stringToCheck => stringToCheck.Contains(myString));
If you simply wish to return the first matching item:
var match = myList
.FirstOrDefault(stringToCheck => stringToCheck.Contains(myString));
if(match != null)
//Do stuff
Recommend
-
8
Different ways to check if a string contains another string in Swift 11 Jan 2021 ⋅ 8 min read ⋅ Swift
-
7
If the string is equal to the item in list a, followed by the item in list b advertisements I'd like to make an if statement to detect if a st...
-
9
ItsMyCode | A substring is a sequence of characters in a given string. Python has several built-in methods that can help find and replace a substring. Following are the options available in Python to check if string contains s...
-
5
In this article, we will learn about three different ways to check if a string contains a character or not. Check if a string contains a character using string::find() In C++, the string class provides different overloaded ver...
-
18
Javascript check if string contains only numbers It is common in javaScript to find ourselves with the problem o...
-
7
Check if a string contains a number in Python In this article, we will discuss different ways to check if a string contains a number or not in Python. Table of Contents Sup...
-
8
How to Check Whether a String Contains a Substring in JavaScript 1270 views 2 years ago Javascript Use the
-
3
JavaScript Check if String Contains a Substring 306 views 7 months ago Javascript JavaScript has rich library of...
-
7
How to Check if a Python String Contains a Substring Remove ads If you’re new to programming or come from a programming language other than Python, you...
-
6
Java program to Check whether the String contains only digitsSkip to content Java pr...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK