[javascript] How to get character array from a string?

How do you convert a string to a character array in JavaScript?

I'm thinking getting a string like "Hello world!" to the array
['H','e','l','l','o',' ','w','o','r','l','d','!']

This question is related to javascript arrays string

The answer is


As hippietrail suggests, meder's answer can break surrogate pairs and misinterpret “characters.” For example:

_x000D_
_x000D_
// DO NOT USE THIS!
const a = ''.split('');
console.log(a);
_x000D_
_x000D_
_x000D_

I suggest using one of the following ES2015 features to correctly handle these character sequences.

Spread syntax (already answered by insertusernamehere)

_x000D_
_x000D_
const a = [...''];
console.log(a);
_x000D_
_x000D_
_x000D_

Array.from

_x000D_
_x000D_
const a = Array.from('');
console.log(a);
_x000D_
_x000D_
_x000D_

RegExp u flag

_x000D_
_x000D_
const a = ''.split(/(?=[\s\S])/u);
console.log(a);
_x000D_
_x000D_
_x000D_

Use /(?=[\s\S])/u instead of /(?=.)/u because . does not match newlines. If you are still in ES5.1 era (or if your browser doesn't handle this regex correctly - like Edge), you can use the following alternative (transpiled by Babel). Note, that Babel tries to also handle unmatched surrogates correctly. However, this doesn't seem to work for unmatched low surrogates.

_x000D_
_x000D_
const a = ''.split(/(?=(?:[\0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))/);
console.log(a);
_x000D_
_x000D_
_x000D_

Reduce method (already answered by Mark Amery)

_x000D_
_x000D_
const s = '';
const a = [];
for (const s2 of s) {
   a.push(s2);
}
console.log(a);
_x000D_
_x000D_
_x000D_


How about this?

function stringToArray(string) {
  let length = string.length;
  let array = new Array(length);
  while (length--) {
    array[length] = string[length];
  }
  return array;
}

The spread Syntax

You can use the spread syntax, an Array Initializer introduced in ECMAScript 2015 (ES6) standard:

var arr = [...str];

Examples

_x000D_
_x000D_
function a() {_x000D_
    return arguments;_x000D_
}_x000D_
_x000D_
var str = 'Hello World';_x000D_
_x000D_
var arr1 = [...str],_x000D_
    arr2 = [...'Hello World'],_x000D_
    arr3 = new Array(...str),_x000D_
    arr4 = a(...str);_x000D_
_x000D_
console.log(arr1, arr2, arr3, arr4);
_x000D_
_x000D_
_x000D_

The first three result in:

["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]

The last one results in

{0: "H", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "W", 7: "o", 8: "r", 9: "l", 10: "d"}

Browser Support

Check the ECMAScript ES6 compatibility table.


Further reading

spread is also referenced as "splat" (e.g. in PHP or Ruby or as "scatter" (e.g. in Python).


Demo

Try before buy


It already is:

_x000D_
_x000D_
var mystring = 'foobar';_x000D_
console.log(mystring[0]); // Outputs 'f'_x000D_
console.log(mystring[3]); // Outputs 'b'
_x000D_
_x000D_
_x000D_

Or for a more older browser friendly version, use:

_x000D_
_x000D_
var mystring = 'foobar';_x000D_
console.log(mystring.charAt(3)); // Outputs 'b'
_x000D_
_x000D_
_x000D_


Array.prototype.slice will do the work as well.

_x000D_
_x000D_
const result = Array.prototype.slice.call("Hello world!");_x000D_
console.log(result);
_x000D_
_x000D_
_x000D_


This is an old question but I came across another solution not yet listed.

You can use the Object.assign function to get the desired output:

_x000D_
_x000D_
var output = Object.assign([], "Hello, world!");_x000D_
console.log(output);_x000D_
    // [ 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!' ]
_x000D_
_x000D_
_x000D_

Not necessarily right or wrong, just another option.

Object.assign is described well at the MDN site.


There are (at least) three different things you might conceive of as a "character", and consequently, three different categories of approach you might want to use.

Splitting into UTF-16 code units

JavaScript strings were originally invented as sequences of UTF-16 code units, back at a point in history when there was a one-to-one relationship between UTF-16 code units and Unicode code points. The .length property of a string measures its length in UTF-16 code units, and when you do someString[i] you get the ith UTF-16 code unit of someString.

Consequently, you can get an array of UTF-16 code units from a string by using a C-style for-loop with an index variable...

_x000D_
_x000D_
const yourString = 'Hello, World!';_x000D_
const charArray = [];_x000D_
for (let i=0; i<=yourString.length; i++) {_x000D_
    charArray.push(yourString[i]);_x000D_
}_x000D_
console.log(charArray);
_x000D_
_x000D_
_x000D_

There are also various short ways to achieve the same thing, like using .split() with the empty string as a separator:

_x000D_
_x000D_
const charArray = 'Hello, World!'.split('');_x000D_
console.log(charArray);
_x000D_
_x000D_
_x000D_

However, if your string contains code points that are made up of multiple UTF-16 code units, this will split them into individual code units, which may not be what you want. For instance, the string '' is made up of four unicode code points (code points 0x1D7D8 through 0x1D7DB) which, in UTF-16, are each made up of two UTF-16 code units. If we split that string using the methods above, we'll get an array of eight code units:

_x000D_
_x000D_
const yourString = '';_x000D_
console.log('First code unit:', yourString[0]);_x000D_
const charArray = yourString.split('');_x000D_
console.log('charArray:', charArray);
_x000D_
_x000D_
_x000D_

Splitting into Unicode Code Points

So, perhaps we want to instead split our string into Unicode Code Points! That's been possible since ECMAScript 2015 added the concept of an iterable to the language. Strings are now iterables, and when you iterate over them (e.g. with a for...of loop), you get Unicode code points, not UTF-16 code units:

_x000D_
_x000D_
const yourString = '';_x000D_
const charArray = [];_x000D_
for (const char of yourString) {_x000D_
  charArray.push(char);_x000D_
}_x000D_
console.log(charArray);
_x000D_
_x000D_
_x000D_

We can shorten this using Array.from, which iterates over the iterable it's passed implicitly:

_x000D_
_x000D_
const yourString = '';_x000D_
const charArray = Array.from(yourString);_x000D_
console.log(charArray);
_x000D_
_x000D_
_x000D_

However, unicode code points are not the largest possible thing that could possibly be considered a "character" either. Some examples of things that could reasonably be considered a single "character" but be made up of multiple code points include:

  • Accented characters, if the accent is applied with a combining code point
  • Flags
  • Some emojis

We can see below that if we try to convert a string with such characters into an array via the iteration mechanism above, the characters end up broken up in the resulting array. (In case any of the characters don't render on your system, yourString below consists of a capital A with an acute accent, followed by the flag of the United Kingdom, followed by a black woman.)

_x000D_
_x000D_
const yourString = 'A´';_x000D_
const charArray = Array.from(yourString);_x000D_
console.log(charArray);
_x000D_
_x000D_
_x000D_

If we want to keep each of these as a single item in our final array, then we need an array of graphemes, not code points.

Splitting into graphemes

JavaScript has no built-in support for this - at least not yet. So we need a library that understands and implements the Unicode rules for what combination of code points constitute a grapheme. Fortunately, one exists: orling's grapheme-splitter. You'll want to install it with npm or, if you're not using npm, download the index.js file and serve it with a <script> tag. For this demo, I'll load it from jsDelivr.

grapheme-splitter gives us a GraphemeSplitter class with three methods: splitGraphemes, iterateGraphemes, and countGraphemes. Naturally, we want splitGraphemes:

_x000D_
_x000D_
const splitter = new GraphemeSplitter();_x000D_
const yourString = 'A´';_x000D_
const charArray = splitter.splitGraphemes(yourString);_x000D_
console.log(charArray);
_x000D_
<script src="https://cdn.jsdelivr.net/npm/[email protected]/index.js"></script>
_x000D_
_x000D_
_x000D_

And there we are - an array of three graphemes, which is probably what you wanted.


The ES6 way to split a string into an array character-wise is by using the spread operator. It is simple and nice.

array = [...myString];

Example:

_x000D_
_x000D_
let myString = "Hello world!"
array = [...myString];
console.log(array);

// another example:

console.log([..."another splitted text"]);
_x000D_
_x000D_
_x000D_


You can iterate over the length of the string and push the character at each position:

_x000D_
_x000D_
const str = 'Hello World';_x000D_
_x000D_
const stringToArray = (text) => {_x000D_
  var chars = [];_x000D_
  for (var i = 0; i < text.length; i++) {_x000D_
    chars.push(text[i]);_x000D_
  }_x000D_
  return chars_x000D_
}_x000D_
_x000D_
console.log(stringToArray(str))
_x000D_
_x000D_
_x000D_


You can also use Array.from.

_x000D_
_x000D_
var m = "Hello world!";
console.log(Array.from(m))
_x000D_
_x000D_
_x000D_

This method has been introduced in ES6.

Reference

Array.from


One possibility is the next:

console.log([1, 2, 3].map(e => Math.random().toString(36).slice(2)).join('').split('').map(e => Math.random() > 0.5 ? e.toUpperCase() : e).join(''));

simple answer:

_x000D_
_x000D_
let str = 'this is string, length is >26';_x000D_
_x000D_
console.log([...str]);
_x000D_
_x000D_
_x000D_


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript