JavaScript Convert String to Array JavaScript

JavaScript Convert String to Array JavaScript

Convert string to array javascript; In this tutorial, you will learn, how to convert strings to arrays & split string by comma separated array javascript with split.

If you want to convert array or object array to comma separated string javascript or convert object array to a comma-separated string javascript. So you can read this post-https://www.tutsmake.com/javascript-array-to-comma-separated-string/

Convert String to Array JavaScript

Before start to convert string to Array in JavaScript with split Method; You must 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 look at examples 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.

You can use the javascript string.split method to split a string into an array. Pass the separator in string.split() method as the first argument, and return new array. 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.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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