-1

if I run this code in a wpf app

TextBox1.Text = trueContent;  //trueContent = "\u5929\u554a"

the testbox will only show the "\u5929\u554a" but if i run the below code

TextBox1.Text = "\u5929\u554a";

I will get the right result so what is the difference between a literal string and a variable string writing in unicode

  • 3
    My bet is that `trueContent = "\\u5929\\u554a"` – Cid Nov 25 '20 at 14:22
  • 3
    Or `@"\u5929\u554a"` – Drag and Drop Nov 25 '20 at 14:22
  • Where did the value of `trueContent` come from? How are you seeing its value? – Hans Kesting Nov 25 '20 at 14:23
  • Even in the IL the Unicode char will be interpreted. The only way to preserve the unicode as typed is to either escape or use the literal @ equivalent https://sharplab.io/#v2:C4LglgNgNAJiDUAfAAgJgIwFgBQyDMABGgQMIEDeOB1RhyALAQLIAUAlBTVwTgJC8A3AIYAnAgBkwwAKYihEAgF4iyAEQAdAK4BWAJypdW7dvpDVAbj6DRBAKIBnAMZCADtJhKCGo/sNGTZpbY/MJiAHIA9iIAtvKeGjq+/qYWVDQAvjjpQA – Drag and Drop Nov 25 '20 at 14:35
  • It's the compiler who processes escape sequences. It sees "\u..." sequence in your string literal - it replaces it with unicode character. If string came from some external source (file etc) - then compiler have nothing to do with it and it stays as is – Evk Nov 25 '20 at 14:40
  • If you need to convert char to it's `\u` unicode equivalent you can use something like https://stackoverflow.com/questions/1615559/convert-a-unicode-string-to-an-escaped-ascii-string – Drag and Drop Nov 25 '20 at 14:41
  • Hey guys, after reading the comment, I debugged the program using visualstudio (the program is debugged using Rider when asking this question) and in visualstudio the content of trueContent is "\\u5929\\u554a" but in Rider is "\u5929\u554a", so as one of the comment says , the trueContent is "\\u5929\\u554a", thank you. That helps me a lot – jichi yan Nov 26 '20 at 01:20

1 Answers1

0

Hey guys, after reading the comment, I debugged the program using visualstudio (the program is debugged using Rider when asking this question) and in visualstudio the content of trueContent is "\u5929\u554a" but in Rider is "\u5929\u554a", so as one of the comment says , the trueContent is "\u5929\u554a", thank you. That helps me a lot