EN
TypeScript - convert string to character array
3
points
In this article, we would like to show you how to convert string to characters array in TypeScript.
Quick solution:
const text: string = 'ABCD';
const array: string[] = text.split('');
Practical examples
Using String split()
method
const text: string = 'ABCD';
const array: string[] = text.split('');
console.log(array);
Output:
[ 'A', 'B', 'C', 'D' ]
Using Array.from()
method
const text: string = 'ABCD';
const array: string[] = Array.from(text);
console.log(array);
Output:
[ 'A', 'B', 'C', 'D' ]
Using Object.assign()
const text: string = 'ABCD';
const array: string[] = Object.assign([], text);
console.log(array);
Output:
[ 'A', 'B', 'C', 'D' ]