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++.
1 Answer
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)));
}
}
3 Comments
zhrgci
I know that
=> is a lambda expression, but I still don't know what it actually means....and where does the v come from? Sorry, kinda new to this.jgoday
Linq's enumerable 'Any' checks if the collection has at least, one value that matchs the function passed as parameter. The function is v => input1.Contains(v). Just one parameter (v with the inferred type from the collection) and the body function is inpu1.Contains(v)
jgoday
It's just a csharp way of write anonymous or lambda functions : learn.microsoft.com/en-US/dotnet/csharp/programming-guide/…