如何部分匹配(startswith)一个元组字符串?

How to partial match (startswith) a tuple string?

是否可以对元组字符串执行 startswith 类型匹配?这是我的精确匹配代码:

if (responseHeaders.Contains(new Tuple<string, string>("Content-Type", "text/html; charset=UTF-8"))) {
    Console.WriteLine("this is a text/html document");
}

但我想匹配任何以 "text/html" 开头的内容类型,而不必完全匹配每个字符集。

我认为您必须遍历它们并分别比较每个项目。类似于:

foreach(var header in responseHeaders)
{
    if (header.Item1 == "Content-Type" && header.Item2.StartsWith("text/html")
    {
        Console.WriteLine("this is a text/html document");
    }
}

或者您可以使用 LINQ 来完成:

if (responseHeaders.Any(x => x.Item1 == "Content-Type" && x.Item2.StartsWith("text/html"))
    //Write Line