如何让我的字符串比较不(忽略)的白色空间的微小差异敏感? [英] How do I make my string compare not sensitive to (ignore) minor differences in white space?
问题描述
我有一些测试,检查显示给用户的字符串。
I have some tests that check strings that are displayed to the user.
我不希望测试失败到由于改变了凹陷或换行等,所以我要寻找类似的字符串比较一下。
I don’t wish the test to fail to due to changes in the indentations or line breaks etc. So I am looking for something like a string compare that.
- 忽略,而空间作为字符串的开始(易用string.trim)
- 当任何数量的任何类型的白色空间的旁边的相互匹配的相同方式,如果它是一个单一的空间。
我可以开始创建一个正则表达式,但其他人可能有一个更好的解决方案,因此这个问题。
I could start to create a regex, but someone else may have a better solution hence this question.
推荐答案
编写自定义比较将是棘手的,如果你需要它的的只是的做到这一点的空白。 我会建议使用正则表达式来规范化,即
Writing a custom compare would be tricky if you need it just to do this for whitespace. I would suggest using regex to normalize, i.e.
private static readonly Regex normalizeSpace =
new Regex(@"\s+", RegexOptions.Compiled);
...
string s = normalizeSpace.Replace(input, " ");
显然正常化两个操作数,然后测试平等是正常的。
Obviously normalize both operands and then test for equality as normal.
这篇关于如何让我的字符串比较不(忽略)的白色空间的微小差异敏感?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!