本文介绍了比较两个字符串时如何不包括换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在比较两个字符串的更新。我做了:
i am comparing updates to two strings. i did a:
string1 != string2
,结果却有所不同。我将它们放在添加手表中,我看到的唯一区别是一个带有换行符,另一个没有换行符。:
and they turn out different. I put them in the "Add Watch" and i see the only difference is one has line breaks and the other doesnt'.:
string1 = "This is a test. \nThis is a test";
string2 = "This is a test. This is a test";
我基本上想进行比较,但不包括换行符。因此,如果只有换行符是唯一的区别,则应将它们视为相等。
i basically want to do a compare but dont include line breaks. So if line break is the only difference then consider them equal.
推荐答案
一种在性能不高的情况下的快速而肮脏的方法问题:
A quick and dirty way, when performance isn't much of an issue:
string1.Replace("\n", "") != string2.Replace("\n", "")
这篇关于比较两个字符串时如何不包括换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!