1

In c# I have

string x = @"\u0044\u0068\uD83D\uDE07\u90FD\u5728"

I need to turn it into:

Dh都在

How do I do that? Specifically, how do I know when \uD83D\uDE07 is one character as compared to two?

  • 2
    just remove the "@" at the beginning and print it. "@" makes it ignore the "\"s. – Eren Ersönmez Jan 27 '16 at 00:23
  • and character modifiers should get handled automatically, you shouldn't have to do anything special. – Eren Ersönmez Jan 27 '16 at 00:25
  • 1
    No @ErenErsönmez, that was just an example. string x contains those characters and I don't have control over how x is set. Unless you have a way to convert x to make it not ignore the escape characters. –  Jan 27 '16 at 00:30
  • Strange -- `` is `U+1F607`, not `U+D83D`. Well: http://unicode.scarfboy.com/?s=%F0%9F%98%87 shows it as `\ud83d\ude07` – Rick James Jan 27 '16 at 00:51
  • I see. No, character literals would be a compile-time thing, can't make them after the fact. You would have to parse the string, as seen in my answer. – Eren Ersönmez Jan 27 '16 at 00:52

2 Answers2

2

You might have to parse each char representation into an int and then convert to a char:

string x = @"\u0044\u0068\uD83D\uDE07\u90FD\u5728";
var chars = x.Split(new[]{@"\u"}, StringSplitOptions.RemoveEmptyEntries)
    .Select(c => (char)Convert.ToInt32(c, 16))
    .ToArray();
var output = new string(chars);
// output = Dh都在
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
0

I know newtonsoft json.net will convert, so I would use that if was one of my projects and I already had references to it:

using Newtonsoft.Json;

var output = new JsonTextReader(new StringReader($"\"{x}\"")).ReadAsString();

//output = Dh都在

Advantages are it's going to cope with nonunicode characters as well, I.e. "Z\u0044" -> "ZD". Disadvantage is that in it's current state there are nonunicode characters it won't cope with, such as quote obviously: "A\"B" will fail.

weston
  • 54,145
  • 21
  • 145
  • 203