drgmak drgmak - 5 months ago 7
Vb.net Question

Get the whole string from a String array if the string contains a substring

I have a string array for example:

string[] myStrings = {"I want to take this string","This is not interesting"}


What i want to check the string array if it contains a value and then return the whole string that contains it.
Example:

var index = Array.FindIndex(stringArray, x => x == stringArray.Contains("want"));


After this i want to return the string:
I want to take this string
since this contains the keyword i searched for.
How can i archive this result?

Answer

I would use LINQ instead:

IEnumerable<string> occurences = stringArray.Where(s => s.Contains("want"));

In occurences you have the complete strings of *all occurences which did match. You can do FirstOrDefault on it if you are just interested in the first hit.

Comments