Navigate back to the homepage

How to convert string to array in typescript?

Infinitbility
TypeScript
October 28th, 2021 · 1 min read
How to convert string to array in typescript?

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.

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.

1let str: string = "0,1,2,3,4";
2
3let arr: array = str.split(",");
4console.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.

1let str: string = "0 1 2 3 4";
2
3let arr: array = str.split(" ");
4console.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.

1let str: string = "01234";
2
3let arr: array = str.split("");
4console.log(arr);

Output

Split, every, elements

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to convert string to number in typescript?

How to convert string to number in typescript?

Example of string to number conversion in typescript

October 28th, 2021 · 1 min read
How to create an array of objects in javascript?

How to create an array of objects in javascript?

Example of create an array of objects in javascript, React, React Native, vue, node, deno, typescript and all javascript framework.

October 27th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]