To convert comma separated string to array JavaScript; Through this tutorial, i am going to show you how to convert strings to arrays & split string by comma separated array in javaScript with split() method.
Remember you have a string, it has any special character, separator, then the string itself is comma separated. so don’t worry. In this tutorial I will show you how to convert your string to array in javaScript by delimeters.
Convert String to Array JavaScript
To convert string to array in JavaScript using split method; as shown below:
Split() Method JavaScript
JavaScript’s string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or a regular expression.
Syntax of JavaScript of Split() Method
The syntax of the function is as follows:
str.split(separator, limit)
Parameter & Values of JavaScript Split() Method
Parameter | Description |
---|---|
separator | Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item) |
limit | Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array |
Example 1 – How to Convert Comma Separated String into an Array in JavaScript
See the following example for To convert comma-separated string into an array in JavaScript; as shown below:
var string = 'hello, world, test, test2, rummy, words'; var arr = string.split(', '); // split string on comma space console.log( arr ); //Output ["hello", "world", "test", "test2", "rummy", "words"]
Example 2 – How to convert string into an array using Separator in javascript
If you do not pass a separator argument to the method to be split, the resulting array will contain a single element that contains the entire string:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var arr = str.split(); // no separator passed to split console.log( arr ); // Output // ["ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
Example 3 – Split() – Empty String Separator:
If you pass an empty string as a separator, each character in the string will create an element in the return array:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var arr = str.split(''); // using the empty string separator console.log( arr ); //(26) ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
Example 4 – Remove Separator and Convert String to Array In JavaScript
If a separator is found at the beginning or end of a string, the first and last elements of the resulting array may be empty strings:
var str = 'A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z'; var arr = str.split('|'); // using the empty string separator console.log( arr ); //(26) ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
Example 5 – Convert String into Array using Regular Expression Separator In JavaScript
I will use separator and it can be a regular expression:
var str = 'favorite desserts: brownies, banana bread, ice cream, chocolate chip cookies'; // regular expression separator var re = /:\s|,\s/; // split on colon space or comma space var ar = str.split(re); console.log( ar ); // [ "favorite desserts", "brownies", "banana bread", "ice cream", "chocolate chip cookies" ]
Example 6 – String Split with Limit Argument in JavaScript
An optional second argument to the partition method sets a limit on the number of elements in a given array:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var arr = str.split('', 5); // with limit console.log( arr ); //(5) ["A", "B", "C", "D", "E"]
Example 6 – How do you split a string, breaking at a particular character or array in javascript
To break a string into a particular character or split string by comma using the javascript split() method; as shown below:
var str = "This-javascript-tutorial-string-split-method-examples." var result = str.split('-'); console.log(result); document.getElementById("show").innerHTML = result;
Conclusion
To split string in javaScript; Through this tutorial, you have learned how to convert comma-separated strings into an array in javascript using the split method. Also, you have learned different technic for convert string into an array in javascript.