0

In textbox1 user enters string "Test\u0021Test" and I would like to convert escaped character "\u0021" to "!"

string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text; // here textbox1.Text = "Test\u0021Test" and this I don't know how to convert

Thanks for help

EDIT Answered by @Simo Erkinheimo Allow user to enter escape characters in a TextBox

Community
  • 1
  • 1
kurin123
  • 363
  • 1
  • 6

2 Answers2

1

Use string Replace method this case

string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text.Replace("\u0021", "!");
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
1

You can do something like this. It will work for all unicode escaped symbols in input string.

var result = Regex.Replace(x, @"\\[u]([0-9a-f]{4})",
match => char.ToString(
    (char)ushort.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)));
Redwan
  • 738
  • 9
  • 28