Reputation: 1614
I Want To Get The Full string If I Founded char In The List
My Code :
List<string> mylist = new List<string>();
// My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse
string text = "";
if (mylist.contains("3"))
{
text = ...//Get The Line That The "3" Is Founded In It
// In This Case text must be : 3-Wolf
}
Thanks,
Upvotes: 0
Views: 53
Reputation: 4895
you can use this code
var text = mylist.FirstOrDefault(x => x.Contains("3"));
// note: text will be **null** if not found in the list
Upvotes: 3
Reputation: 1003
The following should do the service:
List<string> mylist = new List<string>() { "1-Cat", "2-Dog", "3-Wolf", "4-Mouse" };
// My List Contains : 1-Cat , 2-Dog , 3-Wolf , 4-Mouse
string text = "";
text = mylist.FirstOrDefault(x => x.Contains('3')); // text will be 3-Wolf. In case there is no strings in the list that contains 3 text will be null.
// If you want do some other things if the list contains "3" you can do the following:
if (!string.IsNullOrEmpty(text))
{
// Do your stuff here
}
Upvotes: 0