Javascript Split String to Array using Pure JS
Hello dev's, today I'll show you how to split string to array separated by any specific character e.g. ",", "." , "+" etc. in a pure javascript way. Sometimes we require to create an array by given string. So, in that case today's tutorial might be helpful. So, let's get started.
Split String to An Array
For splitting string to array we'll use split() method which takes a argument consisting of special character. Let's see the below code snippet.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>split string to array - shouts.dev</title>
</head>
<body>
<script>
var string = "A+B+C+C+D+E+F+G+H";
console.log(string.split('+'));
</script>
</body>
</html>
Which will produce the below output.
split() is quite handy method for creating arrays from an array. For more info about split() see here.
That's it for today. Hope you've enjoyed this tutorial. Catch me in the comment section if anything goes wrong. Thanks for reading.