Javascript String to Array


StringViews 1325

We may come across situations where we need to convert string to an array of string in order to access individual words. In Javascript, we use the split() function to breakdown a large string to an array of small strings based on separator if required. The separator is nothing but a delimiter with which we want to split the string. Converting String to array using JavaScript’s split method.

JavaScript split() syntax

array = string.split(separator,limit);

string – input value of the actual string

separator – delimiter is used to split the string. This is optional and can be any letter/regular expression/special character.

limit – the number of words that need to be accessed from the array. This is an optional parameter.

array – output value which contains an array of string as the return value

Example to convert String to Array without using separator and limit

Here, we have not used any separator or limit. Hence the array contains only 1 element having the entire string as a single word.

<script>
function splitfunc1() 
{ 
	var str = 'Welcome to javascript tutorial'; 
	var arr = str.split(); 
	print(arr); 
}
 </script>
Output:
[Welcome to javascript tutorial]

Array length: 1

arr[0] – Welcome to javascript tutorial

Split() method example using a delimiter

In the below example, we use blank space as the delimiter to split the string but there is no limit on the number of words to retrieve. Hence the split method returns every word as an array element. In this way, we can convert String to an array using Javascript.

<script>
function splitfunc2() 
{ 
	var str = 'Welcome to javascript tutorial'; 
	var arr = str.split(" "); 
	print(arr); 
}
 </script>
Output:
[Welcome,to,javascript,tutorial]

Array length:  4

arr[0] – Welcome

arr[1] – to

arr[2] – javascript

arr[3] – tutorial

Example to convert JavaScript String to an array using delimiter and limit

If we want to retrieve only 2 words then we specify limit as 2 and use blank space as a separator. This is another way to convert string to array in Javascript. By specifying the limit parameter, we can control the number of words we want to get.

<script>
function splitfunc3() 
{ 
	var str = 'Welcome to javascript tutorial'; 
	var arr = str.split(" ",2); 
	print(arr); 
}
 </script>
Output:
[Welcome,to]

Array length: 2

arr[0] – Welcome

arr[1] – to

JavaScript example to convert String to an array of individual characters

When we need to fetch individual characters in a string, then we pass empty “” as a separator. In this case, every array element will be individual characters.

<script>
function splitfunc4() 
{ 
	var str = 'Welcome to javascript tutorial'; 
	var arr = str.split(""); 
	print(arr); 
}
 </script>
Output:
[W,e,l,c,o,m,e,,t,o,j,a,v,a,s,c,r,i,p,t,,t,u,t,o,r,i,a,l]

Array length: 30 [length of a string including space]

Split method example using a regular expression as separator along with limit

We can also use regular expressions as a separator. We user regular expression when we want to split a string based on 1 or more delimiter.

Below, we have used colon or comma as a delimiter. We store this expression as a variable and pass it as a parameter to split function.

syntax for regular expression: /expression/

<script>
function splitfunc5() 
{ 
	var str = 'programming languages:java,javacript,C,C++'; 
	var reg = /:|,/; 
	var arr = str.split(reg,2);
	print(arr); 
}
 </script>
Output:
[programming languages,java]

Array length: 2

arr[0] – programming languages

arr[1] – java

Conclusion

In this tutorial, we have learned about how to convert string to array in Javascript using the split method along with various examples in detail.

Reference

Translate »