0

I have some code that's returning a null symbol when I try to add unicode to it, anyone know why?

string first = "\uD83D\uDC4A\uD83D\uDC4A\uD83D\uDC4A";
for (int i = 0; i < 20; i++)
{
  Console.Write(first[i % 3] + "\r");
  Thread.Sleep(100);
}
Console.WriteLine("\n");

I'm basically trying to get 3 emojis to changing rapidly, in one character of text, but it's returning null when I use unicode characters.

otisjnz
  • 136
  • 1
  • 15

1 Answers1

3

Emoji symbols are complex, usually an emoji symbol is not a single character but a sequence with variable length.

You can find the specification here.

Here's a gist to split a string into pieces.

var list = new List<string>();
var itor = new EmojiIterator(first);
while(itor.MoveNext())
    list.Add(itor.Sequence);

for (int i = 0; i < 20; i++)
{
    Console.Write(list[i % list.Count] + "\r");
    Thread.Sleep(100);
}

Please notice that I've tested this code on dotnetfiddle, but I'm not sure whether if the VS terminal on Mac can render emojis.


Screenrecord

shingo
  • 18,436
  • 5
  • 23
  • 42
  • Hey! I'm a little confused on what this does. I ran the dotnetfiddle and it just outputs: " r r r r r ". – otisjnz May 04 '22 at 10:00
  • 1
    = `\uD83D\uDC4A`, the website doesn't support `\r`, so it outputs every thing in a line. – shingo May 04 '22 at 10:05
  • I've tried it out but it seems to show one emoji for a breaf second then end the program – otisjnz May 04 '22 at 10:31
  • What's your expectation? – shingo May 04 '22 at 10:33
  • hmmm. the best way I can describe it is that in a single character, the emoji changes every few miliseconds, until the loop ends. Kinda like some sort of casino style change. – otisjnz May 04 '22 at 10:34
  • Did you try your example in the question? If so it only contains one emoji. – shingo May 04 '22 at 10:36
  • I tried `\uD83D\uDC4A\u270c\ufe0f\u270b` which contains the fist, victory hand and regular hand. – otisjnz May 04 '22 at 10:38
  • I don't have a mac, maybe the terminal will buffer the output, you might try "1234" to see if it changes the number. Added a gif to show what I got in vscode's terminal. – shingo May 04 '22 at 11:10
  • 1
    Figured it out! I didn't add a sleep to the program and it instead just set it all at the same time. Thanks shingo! – otisjnz May 04 '22 at 20:02