【问题标题】:string comparison using != is returning true even both strings are the same即使两个字符串相同,使用 != 的字符串比较也会返回 true
【发布时间】:2020-01-27 11:08:11
【问题描述】:

我的 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个字符串是一样的。那么有人可以建议吗?

【问题讨论】:

  • 你可以使用 !String.IsNullOrEmpty("yourString")

标签: c# .net string comparison


【解决方案1】:

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

properties.ItemEventProperties.AfterProperties["ProjectDescription"]:

...Total 18 x 16 cores Win Standard License​&lt;br&gt;already have 5 x 16 cores Win Standard licenses&lt;/p&gt;...

i["ProjectDescription"]:

...Total 18 x 16 cores Win Standard License​&lt;br&gt;----already have 5 x 16 cores Win Standard licenses&lt;/p&gt;...

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

【讨论】:

  • 加上“2016.q123

    It”也不同,那里有非打印字符proof(smaller proof)

  • @Ismael Padilla 抱歉,我忘了删除文本,因为它包含用户名.. 我更新了我的问题.. 这个词已经在 2 文本中
  • 谢谢@MindSwipe,我已经编辑了我的答案。测试您的问题可能与非打印字符有关
  • @IsmaelPadilla 你介意删除“Qu***a”这个词,因为它是一个真实的用户名。谢谢。我输入错了
  • @testtest 看看this diff,你可以看到“2016.q...”部分中有[非打印字符](en.wikipedia.org/wiki/Control_character)导致区别
猜你喜欢
  • 1970-01-01
  • 2016-05-31
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2011-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多