0

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 1

2

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)));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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)
It's just a csharp way of write anonymous or lambda functions : learn.microsoft.com/en-US/dotnet/csharp/programming-guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.