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

标签 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="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 字符串没有。

关于c# - 如何查看一个字符串是否包含另一个带引号的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868395/

相关文章:

c# - .NET 取消异步 I/O 操作

php - 我如何使用 &lt;style&gt; 标签将 css 代码添加到 php 文件,当前方法不起作用。

jquery - 我需要 textarea 来占用剩余的空间

python - 读取文本文件时写入文本文件。 (值错误: I/O operation on closed file.)

android - 在 Android 中检查空电子邮件字符串时出错

sql - 如何在没有 NULL 结果的情况下连接值(Snowflake)

c# - 减去列表C#中的相邻两个值

c# - 具有C#或同等功能的Arduino音频频谱

c# - 使用大量的 `try` 而不是更多的 `if` 是否可以?

javascript - 在 jQuery Multiselect 中自动检查保存的数据