C# 如何查看一个字符串是否包含另一个带引号的字符串?

C# 如何查看一个字符串是否包含另一个带引号的字符串?,c#,html,string,C#,Html,String,我试图查看一个大字符串是否包含以下HTML行: <label ng-class="choiceCaptionClass" class="ng-binding choice-caption">Was this information helpful?</label> 这些信息有用吗? 如您所见,此代码段在多个地方有引用,当我执行以下操作时,它会导致问题: Assert.IsTrue(responseContent.Contains("<label ng-class

我试图查看一个大字符串是否包含以下HTML行:

<label ng-class="choiceCaptionClass" class="ng-binding choice-caption">Was this information helpful?</label>
这些信息有用吗?
如您所见,此代码段在多个地方有引用,当我执行以下操作时,它会导致问题:

Assert.IsTrue(responseContent.Contains("<label ng-class="choiceCaptionClass" class="ng - binding choice - caption">Was this information helpful?</label>"));
Assert.IsTrue(responseContent.Contains(“此信息有用吗?”);
我尝试了以下两种定义字符串的方法:

@"<label ng-class=""choiceCaptionClass"" class=""ng - binding choice - caption"">Was this information helpful?</label>"
@“此信息有用吗?”

“此信息有用吗?”

但是在每种情况下,Contains()方法都会查找带有双引号或反斜杠的文本字符串。是否有其他方法可以定义此字符串以便正确搜索它?

使用反斜杠转义双引号是正确的做法


搜索失败的原因可能是字符串实际上不匹配。例如,在带有反斜杠的版本中,某些破折号周围有空格,但HTML字符串没有空格。

尝试使用正则表达式。我为你做了这个,但是你可以测试你自己的正则表达式

var regex=new regex(@“\s*此信息有用吗\?\s*”,RegexOptions.IgnoreCase);
Assert.IsTrue(regex.IsMatch(responseContent));
如果这不起作用,请使用测试仪工具确定图案的哪一部分脱落


希望这有帮助

试着用一个正规的表现主义使用“不可能”吗?它为您处理搜索/解析HTML。这样你就不必自己滚动了。好主意,我忘了使用敏捷包。我将在我即将到来的工作中包括这一点。
"<label ng-class=\"choiceCaptionClass\" class=\"ng - binding choice - caption\">Was this information helpful?</label>"
var regex = new Regex(@"<label\s+ng-class\s*=\s*""choiceCaptionClass""\s+class\s*=\s*""ng-binding choice-caption""\s*>\s*Was this information helpful\?\s*</label>", RegexOptions.IgnoreCase);
Assert.IsTrue(regex.IsMatch(responseContent));