zhrgci
zhrgci

Reputation: 686

C# Check if string contains any item in stringlist

How do I check if my input-string contains any strings in my string-list? I looked some solutions up, but most of them weren't what I was looking for and others were in Python and C++.

Upvotes: 0

Views: 1236

Answers (1)

jgoday
jgoday

Reputation: 2836

You can use linq and string.Contains

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Program
{
    public static void Main()
    {
        var values = new List<string> { "some", "input", "values" };
        var input1 = "this does not have any match";
        Console.WriteLine("Input1 contains some match? " + values.Any(v => input1.Contains(v)));

        var input2 = "this does have some match";
        Console.WriteLine("Input2 contains some match? " + values.Any(v => input2.Contains(v)));
    }
}

Upvotes: 2

Related Questions