Search code examples
c#listloopsstartswith

Find out if string list items startswith another item from another list


I'd like to loop over a string list, and find out if the items from this list start with one of the item from another list.

So I have something like:

List<string> firstList = new List<string>();
firstList.Add("txt random");
firstList.Add("text ok");
List<string> keyWords = new List<string>();
keyWords.Add("txt");
keyWords.Add("Text");

Solution

  • You can do that using a couple simple for each loops.

    foreach (var t in firstList) {
        foreach (var u in keyWords) {
            if (t.StartsWith(u) {
                // Do something here.
            }
        }
    }