C# string comparison ignoring spaces, carriage return or line breaks

前端 未结 10 960
执念已碎
执念已碎 2020-12-05 13:07

How can I compare 2 strings in C# ignoring the case, spaces and any line-breaks. I also need to check if both strings are null then they are marked as same.

Thanks!<

10条回答
  •  盖世英雄少女心
    2020-12-05 13:37

    First replace all whitespace via regular expression from both string and then use the String.Compare method with parameter ignoreCase = true.

    string a = System.Text.RegularExpressions.Regex.Replace("void foo", @"\s", "");
    string b = System.Text.RegularExpressions.Regex.Replace("voidFoo", @"\s", "");
    bool isTheSame = String.Compare(a, b, true) == 0;
    

提交回复
热议问题