C#检查字符串是否包含stringlist中的任何项目

问题描述 投票:0回答:1

如何检查我的input-string是否包含我的字符串-list中的任何字符串?我看了一些解决方案,但大多数都不是我想要的,而其他的则是Python和C ++。

c# string list windows-forms-designer
1个回答
1
投票

你可以使用linq和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)));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.