user2156181
user2156181

Reputation: 15

I want to show weird code/character on textbox winform

I have no idea how to show weird character code. example \n, \u0003. I want to show it "\n" or "\u0003" on textbox, i.e. I don't want to show the symbol itself but its code.

any idea?

thank you so much

Upvotes: 0

Views: 542

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

If you want to encode the string, i.e. to represent control characters with their codes (for instance for some kind of logging, debugging etc.)

  using System.Linq;

  ...

  private static string Encode(string value) {
    if (null == value)
      return null;

    return string.Concat(value
      .Select(c => char.IsControl(c) 
         ? $"\\u{((int) c):x4}" // control symbol - as a code
         : c.ToString()));      // non-control    - as it is
  }

Then

  myTextBox.Text = Encode("abc\ndef\r\t566");

And you'll have

  abc\u000adef\u000d\u0009566

Upvotes: 1

Related Questions