Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

TypeScript haven’t any new or specific way to convert string to an array , we have to do the same like javascript.

To convert string to array in typescript, use the split() method it will create an array based on your string and separator.

JavaScript provide multiple way to convert string to array, we can use split().

let see how we can use javascript string to array conversion ways in typescript.

Split()

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

Make array using comma , separator

You have a comma separated string and you want to make an array, you will do it like the below example.

let str: string = "0,1,2,3,4";

let arr: Array<string> = str.split(",");
console.log(arr);

Output

Split, comma, example

Make array using space separator

You have space separated string and you want to make an array, you will do it like the below example.

let str: string = "0 1 2 3 4";

let arr: Array<string> = str.split(" ");
console.log(arr);

Output

Split, space, element

Make array elment without any separator

You want to make array elment for every string elment, you will do it like the below example.

let str: string = "01234";

let arr: Array<string> = str.split("");
console.log(arr);

Output

Split, every, elements

I hope it helps you, All the best đź‘Ť.