简体   繁体   English

C#字符串比较失败

[英]C# string comparison failure

My application is failing on a string comparison. 我的应用程序在字符串比较失败。 I have put in a breakpoint and then used the intermediate window of Visual Studio and done the following experiment 我已经放入一个断点然后使用Visual Studio的中间窗口并完成以下实验

subject

"<#MethodResourceObjectives​>"

subject.Contains("<#Method")

true

subject.Contains("<#MethodResource")

true

subject.Contains("<#MethodResourceObjectives")

true

subject.Contains("<#MethodResourceObjectives>")

false

This would seem to be impossible, has anyone got a clue what could be happening? 这似乎是不可能的,有没有人知道可能发生什么?

It sounds like there may well be an unprintable character between the "s" and the ">". 听起来“s”和“>”之间可能存在不可打印的字符。

I usually use something like this to show the true contents of a string: 我通常使用这样的东西来显示字符串的真实内容:

for (int i = 0; i < text.Length; i++)
{
    Console.WriteLine("{0:x4}", (int) text[i]);
}

That's not as convenient from an immediate window, of course :( 当然,这不是一个直接的窗口方便:(

In fact, just copying and pasting your text into my Unicode Explorer (at the bottom of the page), it looks like this is indeed the problem - you've got a U+200B (zero width space) before the > . 事实上,直接复制粘贴文本到我的统一资源管理器 (在页面的底部),它看起来像这确实是问题-你有一个U + 200B之前的(零宽度空间) > You need to work out where that's coming from. 你需要弄清楚它来自哪里。

Doing a copy/paste of the text, I can confirm the same behavior. 复制/粘贴文本,我可以确认相同的行为。

Output: 输出:

"<#MethodResourceObjectives>".ToCharArray()
{char[27]}
    [0]: 60 '<'
    [1]: 35 '#'
    [2]: 77 'M'
    [3]: 101 'e'
    [4]: 116 't'
    [5]: 104 'h'
    [6]: 111 'o'
    [7]: 100 'd'
    [8]: 82 'R'
    [9]: 101 'e'
    [10]: 115 's'
    [11]: 111 'o'
    [12]: 117 'u'
    [13]: 114 'r'
    [14]: 99 'c'
    [15]: 101 'e'
    [16]: 79 'O'
    [17]: 98 'b'
    [18]: 106 'j'
    [19]: 101 'e'
    [20]: 99 'c'
    [21]: 116 't'
    [22]: 105 'i'
    [23]: 118 'v'
    [24]: 101 'e'
    [25]: 115 's'
    [26]: 62 '>'

Then 然后

subject.ToCharArray()
{char[28]}
    [0]: 60 '<'
    [1]: 35 '#'
    [2]: 77 'M'
    [3]: 101 'e'
    [4]: 116 't'
    [5]: 104 'h'
    [6]: 111 'o'
    [7]: 100 'd'
    [8]: 82 'R'
    [9]: 101 'e'
    [10]: 115 's'
    [11]: 111 'o'
    [12]: 117 'u'
    [13]: 114 'r'
    [14]: 99 'c'
    [15]: 101 'e'
    [16]: 79 'O'
    [17]: 98 'b'
    [18]: 106 'j'
    [19]: 101 'e'
    [20]: 99 'c'
    [21]: 116 't'
    [22]: 105 'i'
    [23]: 118 'v'
    [24]: 101 'e'
    [25]: 115 's'
    [26]: 8203 '​'  <--------- input string contains 'garbage'
    [27]: 62 '>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM