C#中的字符串比较忽略空格,回车或换行符 [英] C# string comparison ignoring spaces, carriage return or line breaks

查看:632
本文介绍了C#中的字符串比较忽略空格,回车或换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以比较两个字符串在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.

这篇关于C#中的字符串比较忽略空格,回车或换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆