How do I make my string compare not sensitive to (ignore) minor differences in white space?

前端 未结 4 913
不知归路
不知归路 2020-12-10 06:11

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

4条回答
  •  庸人自扰
    2020-12-10 06:50

    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.

提交回复
热议问题