简体   繁体   中英

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++.

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)));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM