-2

I am calling an API that returns a string with the following information: "abc \\u0000\\u0000 fjkdshf". I have tried removing this using the following code but it doesn't seem to work.

string res = str.Replace("\\", string.Empty)
    .Replace("u0000", string.Empty)
    .Trim();

I have read in a couple of articles that this string doesn't actually display when you are not debugging using Visual Studio so I don't know how to fix this problem. Please help!

Josh Monreal
  • 754
  • 1
  • 9
  • 29
  • 1
    How are you checking this string? The `\\uXXXX` values are usually shorthand for unicode characters. Rather than the text `\\u0000` you may have null terminator characters, and the tool you're using to view the data is showing the unicode value for any unprintable text. – Joel Coehoorn Nov 12 '20 at 14:18
  • 1
    Try `str.Replace("\u0000", string.Empty)` or `str.Replace("\0", string.Empty)`. – 41686d6564 stands w. Palestine Nov 12 '20 at 14:19
  • The string is actually showing on our logs in CosmosDB which I assume shows the unicode value of the null terminator character. How can I remove this? – Josh Monreal Nov 12 '20 at 14:19
  • @41686d6564, it doesn't work because my string contains two backslashes, not one. – Josh Monreal Nov 12 '20 at 14:20
  • 2
    @Josh Did you try it or are you just speculating that it _won't_ work? Note that `"\u0000"` is a null terminator character and it might get displayed as `"\\u0000"` in certain situations. A literal `"\\u0000"` in C#, on the other hand, means "a slash character followed by the letter "u", followed by four zeros. – 41686d6564 stands w. Palestine Nov 12 '20 at 14:21
  • @41686d6564, I tried your solution when debugging VS but it didn't work. I'm going to deploy our application and see if what you said will work. – Josh Monreal Nov 12 '20 at 14:23

1 Answers1

1

you mistake the double backslash as actual characters. They are displayed in the debugger because the second backslash is escaped and the first is used as an escape charachter. if you want to replace it simply use "\u0000".

Here is an examplary programm that prints the UTF code of each character

void Main()
{
    string s = "abc \u0000\u0000 fjkdshf";
    Console.WriteLine(string.Join(" ", s.Select(x => Convert.ToInt32(x))));

    string res = s.Replace("\u0000", string.Empty);
    Console.WriteLine(string.Join(" ", res.Select(x => Convert.ToInt32(x))));       
}

Output:

97 98 99 32 0 0 32 102 106 107 100 115 104 102
97 98 99 32 32 102 106 107 100 115 104 102

as you can see in the output, after the replacement the zeros are gone!

More information about literal string escaping

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76