62

I have two strings.

one is "\""

and the other is "\""

I think that they are same.

However, String.Compare says they are different.

This is very strange.

Here's my code:

string b = "\"";
string c = "\"";

if (string.Compare(b, c) == 0)
{
    Console.WriteLine("Good");
}

if (c.StartsWith("\""))
{
    Console.WriteLine("C");
}

if (b.StartsWith("\""))
{
    Console.WriteLine("B");
}

I expected that it may print "GoodCB".

However, it only prints "B".

In my debugger, c[0] is 65279 '' and c[1] is 34 '"'. and b[0] is '"'.

But I don't know what 65279 '' is.

Is it an empty character?

Ronan Boiteau
  • 8,910
  • 6
  • 33
  • 52
장선민
  • 741
  • 1
  • 6
  • 7

4 Answers4

92

It's a zero-width no-break space.
It's more commonly used as a byte-order mark (BOM).

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
8

If you are using Notepad++, try converting to UTF-8 (no BOM), and also make sure ALL your files in the project are the same file system format.

kurdtpage
  • 2,915
  • 1
  • 22
  • 23
4

If you are reading from a file you have opened in notepad, it may have added it as it is one of several programs notorious for doing so.

Dan Witkowski
  • 311
  • 1
  • 4
  • How can I remove that char when I cannot sure whether it starts with '' or not. – 장선민 Jul 22 '11 at 02:05
  • Notepad and other programs are saving UTF8 files, which is a valid and common format. The BOM only bothers you when you read the file with the wrong encoding. – SLaks Jul 22 '11 at 02:14
  • I want to determine whether '' is exist or not using c[0] == '' but I cannot build this. – 장선민 Jul 22 '11 at 02:31
4

You can remove it with:

Trim(new char[]{'\uFEFF','\u200B'});
Victor
  • 855
  • 1
  • 9
  • 24