For long time we used naive approach to split strings in JS:
someString.split('');
But popularity of emoji forced us to change this approach - emoji characters (and other non-BMP characters) like π are made of two "characters'.
String.fromCodePoint(128514).split(''); // array of 2 characters; can't embed due to StackOverflow limitations
So what is modern, correct and performant approach to this task?
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
Answer: Use the split() Method.
In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.
Unicode is a superset of ASCII and Latin-1 and supports virtually every written language currently used on the planet. ECMAScript 3 requires JavaScript implementations to support Unicode version 2.1 or later, and ECMAScript 5 requires implementations to support Unicode 3 or later.
const str = "ππ€πΈπ";
console.log([...str]);
function split(str){
const arr = [];
for(const char of str)
arr.push(char)
return arr;
}
const str = "ππ€πΈπ";
console.log(split(str));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With