avatarCristian Salcescu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1851

Abstract

st</span> chars = word.split(<span class="hljs-string">''</span>)<span class="hljs-comment">;</span> //[<span class="hljs-string">"H"</span>, <span class="hljs-string">"i"</span>, <span class="hljs-string">"�"</span>, <span class="hljs-string">"�"</span>]</pre></div><p id="1cb5">Instead of getting the emoji as the third element in the new array, we got to unrecognizable characters.</p><p id="8e33">Also, the <code>str.length</code> property is wrong in this case. It says <code>4</code>.</p><p id="6a2b"><code>str.length</code> gives the count of UTF-16 units.</p><div id="7492"><pre><span class="hljs-built_in">word</span>.<span class="hljs-built_in">length</span><span class="hljs-comment"> //4</span></pre></div><h1 id="aa66">Array.from</h1><p id="4060">There is another utility that can help in this scenario the <code>Array.from</code>.</p><p id="2326">The <code>Array.from</code> utility creates a new array from an array-like object.</p><div id="7ea1"><pre><span class="hljs-keyword">const</span> word = <span class="hljs-string">'Lannister'</span><span class="hljs-comment">;</span> <span class="hljs-keyword">const</span> chars = Array.from(word)<span class="hljs-comment">;</span> //[<span class="hljs-string">"L"</span>, <span class="hljs-string">"a"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"i"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"t"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"r"</span>]</pre></div><p id="874b">It handles the case when the characters take more than 16-bit.</p><div id="0ad0"><pre><span class="hljs-keyword">const</span> word = <span class="hljs-string">'Hi🙂'</span><span class="hljs-comment">;</span> <span class="hljs-keyword">const</span> chars = Array.from(word)<span class="hljs

Options

-comment">;</span> //[<span class="hljs-string">"H"</span>, <span class="hljs-string">"i"</span>, <span class="hljs-string">"🙂"</span>]</pre></div><h1 id="7aac">Spread Syntax</h1><p id="744e">The spread syntax can also be used to convert a string into an array of characters. Below is an example of converting a string made of common characters.</p><div id="b438"><pre><span class="hljs-keyword">const</span> word = <span class="hljs-string">'Lannister'</span><span class="hljs-comment">;</span> <span class="hljs-keyword">const</span> chars = [ ...word ]<span class="hljs-comment">;</span> //[<span class="hljs-string">"L"</span>, <span class="hljs-string">"a"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"i"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"t"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"r"</span>]</pre></div><p id="a705">The conversions works also when some characters take more than 16-bit.</p><div id="443d"><pre><span class="hljs-keyword">const</span> word = <span class="hljs-string">'Hi🙂'</span><span class="hljs-comment">;</span> <span class="hljs-keyword">const</span> chars = [...word]<span class="hljs-comment">;</span> //[<span class="hljs-string">"H"</span>, <span class="hljs-string">"i"</span>, <span class="hljs-string">"🙂"</span>]</pre></div><p id="5d6c">To get the correct number of characters in a string containing chars using more the 16-bit we need to convert it into an array, with one of the previous valid methods, and check the length of the new array <code>[...str].length</code> .</p><figure id="9ee2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*khOzofgm9LIypZ_4NApJTA.jpeg"><figcaption></figcaption></figure><p id="5f0d">Thanks for reading.</p></article></body>

How to Convert a String Into an Array of Characters

The split string method, Unicode characters, Array.from utility

Photo by the author

In this short post, we will discuss how to convert strings into an array of characters and how to handle the special case of characters taking more the 16-bits.

There is no char type in JavaScript. A character is still a string containing a single character.

split

To convert a string into an array storing all its characters we can use the split method for example.

const word = 'Lannister';
const chars = word.split('');
//["L", "a", "n", "n", "i", "s", "t", "e", "r"]

Strings are store in UTF-16 units in JavaScript. The split method works well for all characters fitting in 16 bits but has issues when trying to split a character that takes more than that, like an emoji. Consider the following example.

const word = 'Hi🙂';
const chars = word.split('');
//["H", "i", "�", "�"]

Instead of getting the emoji as the third element in the new array, we got to unrecognizable characters.

Also, the str.length property is wrong in this case. It says 4.

str.length gives the count of UTF-16 units.

word.length
//4

Array.from

There is another utility that can help in this scenario the Array.from.

The Array.from utility creates a new array from an array-like object.

const word = 'Lannister';
const chars = Array.from(word);
//["L", "a", "n", "n", "i", "s", "t", "e", "r"]

It handles the case when the characters take more than 16-bit.

const word = 'Hi🙂';
const chars = Array.from(word);
//["H", "i", "🙂"]

Spread Syntax

The spread syntax can also be used to convert a string into an array of characters. Below is an example of converting a string made of common characters.

const word = 'Lannister';
const chars = [ ...word ];
//["L", "a", "n", "n", "i", "s", "t", "e", "r"]

The conversions works also when some characters take more than 16-bit.

const word = 'Hi🙂';
const chars = [...word];
//["H", "i", "🙂"]

To get the correct number of characters in a string containing chars using more the 16-bit we need to convert it into an array, with one of the previous valid methods, and check the length of the new array [...str].length .

Thanks for reading.

JavaScript
Javascript Tips
Front End Development
Web Development
Programming
Recommended from ReadMedium