Skip to main content

JavaScript String Split Example Using Split() Method

In this tutorial, I am going to explain How To Split String in JavaScript.The JavaScript have a split() method to break the string into the array.

We’ll use JavaScript to construct a few different scenarios for splitting strings into arrays. This method helps in the converting of a string to an array.The split() method does not change the original string.

The split() method returns an array of strings, that is formed after splitting a given string at each point where the separator occurs.

The split method syntax is –

string.split(separator, limit)

string is the source string variable, separator is the delimiter to split source string and limit defines an upper limit on the number of splits to be found in a given string. Items after the split limit will not be included in the return array.

The Sample Example of JavaScript Split String Method

You can use split() method numerous ways to break a string into parts, This split JavaScript tutorial help to understand usage with an example –

Split() Without Separator

Let’s split a string without using a separator. The source string will be returned to its original state.

var str = "Hello, I am adam.";
var res = str.split();

Output: Hello, I am adam.

Split() With Separator

We’ll use a separator to split a string. The code below uses a space separator to separate the text.

var str = "Hello I am adam.";
var res = str.split(" ");

Output: Hello, I,am,adam.

Split() With Separator and Limit

We may also pass a limit parameter to limit the length of the returned results.

var str = "Hello I am adam.";
var res = str.split(" ", 2);

Output: Hello,I

Some Common Example Of Split String

Let’s have a look at some frequent circumstances that arise during web application development.

JavaScript split a string into an array

This is the default behavior of JavaScript split() method. The split() method is used to split a string into an array of sub-strings and returns the new array.

var str = "Hello I am adam.";
var res = str.split(" ");
console.log(res)

Output: (4) [ “Hello” , “I” , “am” , “adam.” ]

I have split string by space.

JavaScript Split String By Comma

The string has a spacial character like a comma or CSV file which has comma-separated data. We can also split the string by comma.

var str = "Hello, I am adam. I am living in Denver, which is located at Colorado state";
var res = str.split(",");
console.log(res)

The result is :

(3) [
"Hello" ,
" I am adam.I am living in Denver" ,
" which is located at Colorado state"
]

JavaScript Split HEX String

The blow code help to split the hex string. The method are using regex to split strings.

function getRGB(hexVal) {

    return hexVal.match(/[0-9a-f]{2}/gi);

}

JavaScript Split key value String

Sometimes, We need to split key-value object string into the array, We just split the string by comma and convert it into the array.

var str = "key1:value1, key2:value2";
var res = str.split(",");

console.log(res)
//Results (2) [
"key1:value1" ,
" key2:value2"
]

Leave a Reply

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