November 26, 2020

How javascript split string into an array?

In javascript, splitting a string into array by separator is made easy by String prototype method. Here we see How javascript split a string into an array

String.prototype.split(seperator,limiter)

split method returns an array of substrings

for eg.

var demoString = 'Hello Brother, how are you?';
var splittedString = demoString.split(' ');
console.log(splittedString);//["Hello", "Brother,", "how", "are", "you?"]

In the above example, we split string and we got array substring which is separated by space

what if you just want [‘Hello’,’Brother’] instead the whole array of substring then we can use the second parameter of split method which is used as a limiter

for eg.


var demoString = 'Hello Brother, how are you?';

var splittedString = demoString.split(' ',2);
console.log(splittedString);//["Hello", "Brother,"]

we passed 2 as parameter it means it will return first two values of the array

Please subscribe for web development tutorials

What If, if we don’t pass parameter?

Split method returns array of whole string. If no parameter passed

for eg.


var demoString = 'Hello Brother, how are you?';

var splittedString = demoString.split();
console.log(splittedString); //["Hello Brother, how are you?"]

Some usecases of split method

For eg, You have a big paragraph and which have some special characters which you don’t want then split method comes for the rescue


var demoString = "Lorem Ipsum is simply $$$ dummy text of the printing $$$ and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions$$$ of Lorem Ipsum.";


var splittedString = demoString.split('$$$');
console.log(splittedString);

In example given, As you can see we have paragraph with special character $$$ in between paragraph which you don’t want, So we splitted with ‘$$$’

But I know what is next question, we get output as array of substring. But requirement is paragraph. so here we will use join method of array which works exactly opposite of split In which it joins array by using given seperator and output it as a whole string

for eg.


var demoString = "Lorem Ipsum is simply $$$ dummy text of the printing $$$ and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions$$$ of Lorem Ipsum.";


var splittedString = demoString.split('$$$');
console.log(splittedString.join('')); 

So above code will result into a string without “$$$“.

Conclusion:

So, string split has very strong usecase in javascript programming, where we can split string into by using any kind of seperator.Seperator can be character, space, special charater etc.

Leave a Reply

Your email address will not be published. Required fields are marked *