25

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

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?

3
  • 5
    Perhaps the ">" are different characters which have the same visual representation? Commented Jan 6, 2015 at 10:04
  • 2
    maybe he is lying to you! Commented Jan 6, 2015 at 10:06
  • @joe, For fix to your issue, you can change the value(constant value) to which the subject value is compared with as it's containing unprintable characters, as mentioned by others. Commented Jan 6, 2015 at 10:45

2 Answers 2

57

It sounds like there may well be an unprintable character between the "s" and the ">".

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 >. You need to work out where that's coming from.

Sign up to request clarification or add additional context in comments.

6 Comments

There is. I copied and pasted his string in to dotnetfiddle to check and there is a character before the > that shows up.
@leppie, why would subject.Contains("<#MethodResourceObjectives>") == false for any (printable/non-printable) symbol after > ?
@Sinatr: Because the constant string could have the unprintable character at the end and the test will fail.
@leppie, oh, you mean unprintable char in Contains function call itself. But than it can be anywhere in last test, because it's unprintable =P
s>"-s​>" s%3E%22-s%u200B%3E%22
|
27

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 '>'

1 Comment

I added this as a community wiki as Jon Skeet already pointed out the issue. Was too long for a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.