即使两个字符串相同,使用 != 的字符串比较也会返回 true

string comparison using != is returning true even both strings are the same

提问人:John John 提问时间:1/27/2020 最后编辑:John John 更新时间:1/27/2020 访问量:96

问:

我的 asp.net 应用程序中有以下代码:

if (i["ProjectDescription"] != null && properties.ItemEventProperties.AfterProperties["ProjectDescription"] != null && (Regex.Replace(i["ProjectDescription"].ToString(), "<.*?>", String.Empty) != Regex.Replace(properties.ItemEventProperties.AfterProperties["ProjectDescription"].ToString(), "<.*?>", String.Empty)))
 {
    var g = Regex.Replace(i["ProjectDescription"].ToString(), "<.*?>", String.Empty);
    var gg = Regex.Replace(properties.ItemEventProperties.AfterProperties["ProjectDescription"].ToString(), "<.*?>", String.Empty);
}

现在properties.ItemEventProperties.AfterProperties["ProjectDescription"] =

<div class=\"ExternalClassB4424BCE886A43C980835358CB67D0E8\"><p>This project will be to update Win Server 2008 or older server OS to Win Server 2016.q​123​​<br><br></p><p>It will exclude Win Server 2012.&#160;</p><p>Check attached spreadsheet in draft for list of servers and check the POF&#160;</p><p>Licenses&#58;</p><p>Tere will&#160; be a total of 9 Server VMs upgraded on-premises that require Win Server Licenses.&#160;<br></p><p>There are also 2 existing VMs running on Win Server 2012.<br></p><p>This will require&#160;​6 x (16 cores licenses x 3 hosts) ---&gt; Total 18 x 16 cores Win Standard License​<br>already have 5 x 16 cores Win Standard licenses</p><p>We will need to quote and order for additional 13​ x&#160; Win Server Standard16 cores licenses​<br></p><p>The VM's running on Cloud will be licensed through monthly hosting costs</p><p><strong>Cost Summary&#58;​</strong><br>Licenses - £12,612.60<br>Engineer Resources - £7,880.00<br></p></div>

i["ProjectDescription"]=

<div class=\"ExternalClassB4424BCE886A43C980835358CB67D0E8\"><p>This project will be to update Win Server 2008 or older server OS to Win Server 2016.q​123<br><br></p><p>It will exclude Win Server 2012.&#160;</p><p>Check attached spreadsheet in draft for list of servers and check the POF&#160;</p><p>Licenses&#58;</p><p>Tere will&#160; be a total of 9 Server VMs upgraded on-premises that require Win Server Licenses.&#160;<br></p><p>There are also 2 existing VMs running on Win Server 2012.<br></p><p>This will require&#160;​6 x (16 cores licenses x 3 hosts) ---&gt; Total 18 x 16 cores Win Standard License​<br>already have 5 x 16 cores Win Standard licenses</p><p>We will need to quote and order for additional 13​ x&#160; Win Server Standard16 cores licenses​<br></p><p>The VM's running on Cloud will be licensed through monthly hosting costs</p><p><strong>Cost Summary&#58;​</strong><br>Licenses - £12,612.60<br>Engineer Resources - £7,880.00<br></p></div>

此外,结果将重新运行以下内容:-Regex

This project will be to update Win Server 2008 or older server OS to Win Server 2016.q​123It will exclude Win Server 2012.&#160;Check attached spreadsheet in draft for list of servers and check the POF&#160;Licenses&#58;Tere will&#160; be a total of 9 Server VMs upgraded on-premises that require Win Server Licenses.&#160;There are also 2 existing VMs running on Win Server 2012.This will require&#160;​6 x (16 cores licenses x 3 hosts) ---&gt; Total 18 x 16 cores Win Standard License​ already have 5 x 16 cores Win Standard licensesWe will need to quote and order for additional 13​ x&#160; Win Server Standard16 cores licenses​The VM's running on Cloud will be licensed through monthly hosting costsCost Summary&#58;​Licenses - £12,612.60Engineer Resources - £7,880.00

但是上面的字符串比较将返回 true,尽管 2 个字符串是相同的。那么谁能建议呢?

C# .NET 字符串 比较

评论

1赞 lazizanie 1/27/2020
你可以用 !String.IsNullOrEmpty(“你的字符串”)

答:

3赞 Ismael Padilla 1/27/2020 #1

字符串是不一样的。该单词在第一个字符串中缺失,但在第二个字符串中存在(我按照 OP 的要求删除了该单词,因为它是一个实际的用户名)。----

properties.ItemEventProperties.AfterProperties["ProjectDescription"]:

...Total 18 x 16 cores Win Standard License​<br>already have 5 x 16 cores Win Standard licenses</p>...

i["ProjectDescription"]:

...Total 18 x 16 cores Win Standard License​<br>----already have 5 x 16 cores Win Standard licenses</p>...

正如 MindSwipe 在评论中所说,周围存在差异,因为那里似乎有非打印字符。2016.q123<br><br></p><p>It

评论

1赞 MindSwipe 1/27/2020
加上“2016.q123<br><br></p><p>It”也不同,有非印刷字符有校样较小的校样)
0赞 John John 1/27/2020
@Ismael Padilla 对不起,我忘了删除文本,因为它包含一个用户名。我更新了我的问题..这个词已经在 2 文本中
0赞 Ismael Padilla 1/27/2020
谢谢你@MindSwipe,我已经编辑了我的答案。测试您的问题可能与非打印字符有关
0赞 John John 1/27/2020
@IsmaelPadilla您介意删除“Qu***a”一词,因为它是一个真实的用户名。谢谢。我错误地输入了它
1赞 MindSwipe 1/27/2020
@testtest 看看这个差异,你可以看到“2016.q...”部分中有[非打印字符](en.wikipedia.org/wiki/Control_character)导致了差异