4

I have a string variable. And it contains the text:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

When I try to add it to the TextBox control, nothing happens.Because \0 mean END.

How do I add text as it is?

UPDATE: The text is placed in the variable dynamically.Thus, @ is not suitable.

user348173
  • 8,818
  • 18
  • 66
  • 102

6 Answers6

6
"\\0#«Ия\\0ьw7к\\b\\0E\\0њI\\0\\0ЂЪ\\n"

or

@"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n"
xcud
  • 14,422
  • 3
  • 33
  • 29
6

Is the idea that you want to display the backslashes? If so, the backslashes will need to be in the original string.

If you're getting that text from a string literal, it's just a case of making it a verbatim string literal:

string text = @"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n";

If want to pass in a string which really contains the Unicode "nul" character (U+0000) then you won't be able to get Windows to display that. You should remove those characters first:

textBox.Text = value.Replace("\0", "");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

Well, I don't know where your text is coming from, but if you have to, you can use

using System.Text.RegularExpressions;

...

string escapedText = RegEx.Escape(originalText);

However, if it's not soon enough, the string will already contain null characters.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
2

And it contains the text:

\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n

No it doesn't. That's what the debugger told you it contains. The debugger automatically formatted the content as though you had written it as a literal value in your source code. The string doesn't actually contain the backslashes, they were added by the debugger formatter.

The string actually contains binary zeros. You can see this for yourself by using string.ToCharArray(). You cannot display this string as-is, you have to get rid of the zeros. Displaying the content in hex could work for example, BitConverter.ToString(byte[]) helps with that.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You can't.
Standard Windows controls cannot display null characters.

If you're trying to display the literal text \0, change the string to start with an @ sign, which tells the compiler not to parse escape sequences. (@\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n")

If you want to display as much of the string as you can, you can strip the nulls, like this:

textBox.Text = someString.Replace("\0", "");

You can also replace them with escape codes:

textBox.Text = someString.Replace("\0", @"\0");
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You might try escaping the backslash in \0, i.e. \\0. See this MSDN reference for a full list of C# escape sequences.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41