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:
xxxxxxxxxx
1
const text: string = 'ABCD';
2
const array: string[] = text.split('');
xxxxxxxxxx
1
const text: string = 'ABCD';
2
const array: string[] = text.split('');
3
4
console.log(array);
Output:
xxxxxxxxxx
1
[ 'A', 'B', 'C', 'D' ]
xxxxxxxxxx
1
const text: string = 'ABCD';
2
const array: string[] = Array.from(text);
3
4
console.log(array);
Output:
xxxxxxxxxx
1
[ 'A', 'B', 'C', 'D' ]
xxxxxxxxxx
1
const text: string = 'ABCD';
2
const array: string[] = Object.assign([], text);
3
4
console.log(array);
Output:
xxxxxxxxxx
1
[ 'A', 'B', 'C', 'D' ]