JavaScript: Convert String to Array JavaScript

JavaScript: Convert String to Array JavaScript

In this tutorial, we will discuss how to convert strings to an array using javascript or split string by a comma. Here you will learn how to convert different types of stings in javascript using the split() method.

Convert String to Array JavaScript

Before we start to convert string to Array in JavaScript using split Method Know about the basics of Split() method

Split() Method JavaScript

The JavaScript string splitting method returns an array of substrings obtained by splitting a string on the divider you specify. The separator can be a string or a regular expression.

Note

If an empty string (“”) is used as the separator, the string is split between each character.

The split() method does not change the original string.

Syntax

 The syntax of the function is as follows:

str.split(separator, limit) 

Parameter And Values

ParameterDescription
separatorOptional. 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)
limitOptional. An integer that specifies the number of splits, items after the split limit will not be included in the array

Various Example Below:-

Let’s take a various example for convert string to array JavaScript with different technics.

How to Convert Comma Separated String into an Array in JavaScript?

Let’s take a new example for convert comma-separated string into an array in JavaScript.

Insert the splitting method on the string method you want to split into array elements. Pass the separator you want to use to split the string as the first argument, and a new array will return. A simple example shows:

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"]

<html>
<head>
<title>How to Convert Comma Separated String into an Array in JavaScript?</title>
</head>
<body>

<script type="text/javascript">
 var string = 'hello, world, test, test2, rummy, words';
 var arr = string.split(', '); // split string on comma space
 console.log( arr );
</script>
</body>
</html>

Let’s Take Separator Varieties Examples


Split() – No Separator:

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"]

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"]

Split() – Separator at Beginning/End:

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"]

Regular Expression Separator:

In this example, we 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" ]

Capturing Parentheses:

The separator is usually not included in the array returned by the split method. However, if your regular expression separator involves capturing parentheses, the separators will be included in the separate elements of the resulting array:

var str = 'favorite desserts: brownies, banana bread, ice cream, chocolate chip cookies';
var re = /(:\s|,\s)/; // regular expression with capturing parentheses
var ar = str.split(re);
console.log( ar );
// [ "favorite desserts", ": ", "brownies", ", ", "banana bread", ", ", "ice cream", ", ", "chocolate chip cookies" ]

Split() with Limit Argument

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"]

How do you split a string, breaking at a particular character or array in javascript?

In this example, we have a string, we will break a string into a particular character or split string by comma using the javascript split() method.

    var str = "This-javascript-tutorial-string-split-method-examples."

    var result = str.split('-'); 
    
    console.log(result);
    
    document.getElementById("show").innerHTML = result; 

Source code:

<html>
<head>
<title>How do you split a string, breaking at a particular character in javascript?</title>
</head>
<body>

<p id="show"></p> 

<script> 

    var str = "This-javascript-tutorial-string-split-method-examples."

    var result = str.split('-'); 
    
    console.log(result);
    
    document.getElementById("show").innerHTML = result; 
  
</script> 

</body>
</html>


Conclusion

In this javascript split method 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.

You may like

  1. JavaScript Array splice() Method By Example
  2. Sorting Using JavaScript Array Sort() Method
  3. JavaScript: Clone an Array & Object By Examples
  4. Remove (First, last, specific) Element From Array Javascript
  5. javaScript Push() Array By Example
  6. javaScript Push Array, Items Into Array Example
  7. JavaScript Check the Object | isArray() Function
  8. JavaScript: Important Array Methods

Leave a Reply

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