![](/img/trans.png)
[英]How do I check to see if any part of a string[] contains any part of another string[]?
[英]How do I see if a string contains another string with quotes in it?
我試圖看一個大字符串是否包含這行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>"));
我嘗試了這兩種定義字符串的方法:
@"<label ng-class=""choiceCaptionClass"" class=""ng - binding choice - caption"">Was this information helpful?</label>"
和
"<label ng-class=\"choiceCaptionClass\" class=\"ng - binding choice - caption\">Was this information helpful?</label>"
但在每種情況下,Contains()方法都會使用雙引號或反斜杠查找文字字符串。 有沒有其他方法可以定義此字符串,以便我可以正確搜索它?
用反斜杠轉義雙引號是正確的做法。
您的搜索可能失敗的原因是字符串實際上不匹配。 例如,在帶有反斜杠的版本中,某些破折號周圍有空格,但HTML字符串卻沒有。
嘗試使用正則表達式。 我為你制作了這個,但你可以在這里測試你自己的正則表達式。
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));
如果這不起作用,請使用測試工具弄清楚模式的哪個部分正在下降。
希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.