JavaScript String split()

The split function in JavaScript divides a string into an array of substrings, places them in an array, and then provides this new array as the output without altering the original string.

In cases where the input string is empty, the split function will yield an array containing a single element - an empty string. Only when both the input string and the separator are empty strings will the split method return an empty array.

Syntax

Example

string.split(separator, limit)

The function arguments are discussed as follows.

The "separator" parameter is not mandatory and can either be a regular expression or a basic string. It indicates the exact position where the splitting of the string should occur.

When dealing with strings containing multiple characters, it is essential to identify the entire character sequence in order to perform splitting operations.

When the separator is absent or not specified within the provided string, the string as a whole is treated as a single element in the array. Consequently, the resulting array will comprise a solitary element that represents the complete string.

In case the separator is located at the start or end of the string, it will still cause a split. The resulting array will include an empty string with a length of zero, positioned at the start or end of the array.

The parameter "limit" is optional and represents a non-negative integer indicating the maximum number of splits to perform on a given string. When provided, it dictates the upper limit for the number of splits to be identified based on a specified separator. The string will be divided at each instance of the separator until the limit is reached, at which point the splitting process will stop and the entries will be placed into an array.

A scenario where an array may have less elements than a specified limit occurs when the end of the string is encountered before the limit is reached.

Let's explore how the split function works through a few illustrative examples.

Example1

In this instance, the split method divides the str string at each occurrence of a space (" ") and provides an array of string elements. In this scenario, a specific limit of 3 is set by passing it as an argument to the function.

Example

<!DOCTYPE html>

<html>

<head>

<script>

var str = 'Welcome to the logic-practice.com'

var arr = str.split(" ", 3);

document.write(arr);

</script>

</head>

<body>



</body>

</html>

Output

Output

Welcome,to,the

Example2

In this instance, we are employing the character 't' to serve as the delimiter within the provided string. By utilizing the split method, an array of substrings will be generated through the process of dividing the specified string whenever the character 't' is encountered.

Here, we are not specifying the limit argument.

Example

<!DOCTYPE html>

<html>

<head>

<script>

var str = 'Welcome to the logic-practice.com'

var arr = str.split("t");

document.write(arr);

</script>

</head>

<body>



</body>

</html>

Output

Output

Welcome ,o ,he C# TutorialTec,.com

Example3

In this instance, the separator parameter is being excluded. As a result, the output will display an array with only one element, which is the provided string.

Example

<!DOCTYPE html>

<html>

<head>

<script>

var str = 'Welcome to the logic-practice.com'

var arr = str.split();

document.write(arr);

</script>

</head>

<body>



</body>

</html>

Output

Output

Welcome to the logic-practice.com

Example4

In this context, we are setting the limit parameter and employing the character 'o' as the delimiter in the provided string. The split method will generate a string array by dividing the provided string at each instance of the specified character 'o' up to the defined limit.

The limit parameter restricts the splitting to a maximum of two occurrences by setting 2 as the limit value. When observing the output, we can observe that the splitting occurs precisely two times.

Example

<!DOCTYPE html>

<html>

<head>

<script>

var str = 'Welcome to the logic-practice.com'

var arr = str.split("o", 2);

document.write(arr);

</script>

</head>

<body>



</body>

</html>

Output

Output

Welc,me t

Input Required

This code uses input(). Please provide values below: