0

I have string like that:

enter image description here

Now I have loop that removes 1st character if this first character's numeric value 65279

while (!string.IsNullOrEmpty(json) && json[0] == 65279)
{
    json = json.Substring(1, json.Length - 1);
}

return json;

I have break point at first { so before Substring is executed

and now in watch window I type json[0] == 65279 and it returns FALSE despite that it must have returned TRUE in order to enter that loop!

enter image description here

what's going on here?

BTW: I'm aware that the same goal can be achieved with

.TrimStart(new char[] { Convert.ToChar(65279) });

instead of loop

Repro code:

var json = $"{Convert.ToChar(65279)}{Convert.ToChar(65279)}{{";
Console.WriteLine(json[0] == 65279);
Joelty
  • 1,751
  • 5
  • 22
  • 64
  • ascii 65279 ? an ascii is always< 255 – Frenchy Feb 17 '21 at 12:43
  • @Frenchy fixed that `ASCII` => `character's numeric value` – Joelty Feb 17 '21 at 12:45
  • @Selvin `var myString = Convert.ToChar(65279).ToString();` `var myChar = Convert.ToChar(65279);` `Console.WriteLine(myString.Length);` `Console.WriteLine(myChar);` `Console.WriteLine((int)myChar);` try that – Joelty Feb 17 '21 at 12:49
  • json is unicode, so you have to serialize it – Frenchy Feb 17 '21 at 12:54
  • I want to deserialize it, but `Newtonsoft.Json` FAILs to parse that because of those two characters at the beginning – Joelty Feb 17 '21 at 12:54
  • https://stackoverflow.com/questions/6784799/what-is-this-char-65279 – mjwills Feb 17 '21 at 13:00
  • Please share a [mcve]. Screenshots aren't helpful. – mjwills Feb 17 '21 at 13:00
  • https://developercommunity.visualstudio.com/content/problem/1306238/incorrect-surrogate-char-comparison-result-in-watc.html – mjwills Feb 17 '21 at 13:15
  • The value I suspect you want to test with is `"\uFEFF"`. rather than `"\u65279"` @Selvin. – mjwills Feb 17 '21 at 13:16
  • 1
    This has been borken for quite a while already. Tools > Options > Debugging > General, tick "Use the legacy C# and VB expression evaluators". But doesn't fix it for .NETCore/.NET5 projects, hopefully a good reason for them to finally fix the bugs. – Hans Passant Feb 17 '21 at 13:29
  • @mjwills added the code to repro – Joelty Feb 19 '21 at 08:34
  • instead of `Convert.ToChar(65279)` just use `'\uFEFF'`, `'\xFEFF'` or `(char)0xFEFF`, much shorter and more readable. 0xFEFF is famous but no one knows what the magic number 65279 means – phuclv Feb 19 '21 at 09:01

0 Answers0