C#中的字符串比较忽略空格,回车或换行符空格、字符串、换行符

2023-09-02 11:58:03 作者:心若向阳,无畏悲伤

我如何可以比较两个字符串在C#中忽略的情况下,空格以及任何的换行符。我也需要检查,如果两个字符串都为null,则它们标记为相同的。

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.

谢谢!

推荐答案

删除所有你不想要的字符,然后使用TOLOWER()方法来忽略大小写。

Remove all the characters you don't want and then use the ToLower() method to ignore case.

编辑:虽然上述作品,最好使用 StringComparison.OrdinalIgnoreCase 。只是把它作为第二个参数等于方法。

edit: While the above works, it's better to use StringComparison.OrdinalIgnoreCase. Just pass it as the second argument to the Equals method.