0

In my code I'm trying to isolate out the first character of a variable, it is the UTF8 symbol: The code to outputs are as follows:

Code:

 console.log(login_name);
 console.log(login_name.charAt(0));
 console.log(login_name.substring(0,1));

Output:

  ✨✨✨UTF8MB4
 �
 �

Obviously, I want .charAt() to print and not �. Any known oddities with utf8mb4 that I'm missing? My main problem is I don't know how to word this specific problem.

Also if I swap the rainbow for/ target the ✨, it functions as it should and prints properly.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Auklin
  • 13
  • 1
  • 1
    Does this answer your question? [Javascript charAt() breaking multibyte character string](https://stackoverflow.com/questions/19752988/javascript-charat-breaking-multibyte-character-string) – gforce301 Jan 20 '20 at 22:39

1 Answers1

2

JavaScript can't handle Unicode properly. charAt() operates on code units instead of code points.

Luckily JavaScript has workarounds. To get the characters in a string instead of UTF-16/UCS-2 code units you need to call Array.from(yourstring), which will get you an array of characters. From there on you can get the first element in the usual way.

let characters = Array.from(login_name);
console.log(characters.shift());
Dharman
  • 30,962
  • 25
  • 85
  • 135