JavaScript String split() Method
Last Updated :
26 Jun, 2024
JavaScript split() method proves invaluable for breaking down strings into arrays of substrings, utilizing a designated separator as a reference point.
This method breaks the original string into segments wherever the separator occurs, generating an array where each element corresponds to a distinct substring. By using split(), developers gain a powerful tool for parsing and organizing string data according to specific delimiters or patterns.
Syntax:
str.split(separator, limit);
Parameters:
- separator: It is used to specify the character, or the regular expression, to use for splitting the string. If the separator is unspecified then the entire string becomes one single array element. The same also happens when the separator is not present in the string. If the separator is an empty string (“”) then every character of the string is separated.
- limit: Defines the upper limit on the number of splits to be found in the given string. If the string remains unchecked after the limit is reached then it is not reported in the array.
Return value:
This function returns an array of strings that is formed after splitting the given string at each point where the separator occurs.
Example: Here is the basic example of the split() method.
JavaScript
// JavaScript Program to illustrate split() function
function func() {
//Original string
let str = 'Geeks for Geeks'
let array = str.split("for");
console.log(array);
}
func();
Output[ 'Geeks ', ' Geeks' ]
Explanation:
- JavaScript program defines function func() splitting string ‘Geeks for Geeks’ using “for” as separator.
- Resulting array [“Geeks “, ” Geeks”] is logged to console.
Example: Here, the function split() creates an array of strings by splitting str wherever ” ” occurs.
JavaScript
// JavaScript Program to illustrate split() function
function func() {
//Original string
let str = 'It iS a 5r&e@@t Day.'
let array = str.split(" ");
console.log(array);
}
func();
Output[ 'It', 'iS', 'a', '5r&e@@t', 'Day.' ]
Explanation:
The string ‘It iS a 5r&e@@t Day.’ is split into an array using a space as the separator.
- Resulting array: [“It”, “iS”, “a”, “5r&e@@t”, “Day.”].
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browsers:
JavaScript String Split() methods- FAQs
What is the split() method in JavaScript, and what is its primary use?
The split() method in JavaScript is used to divide a string into an array of substrings, using a specified separator as the reference point. This method is valuable for parsing and organizing string data according to specific delimiters or patterns. When a string is split, each element in the resulting array corresponds to a distinct substring separated by the specified delimiter.
How does the separator parameter affect the behavior of the split() method?
The separator parameter in the split() method determines where the string will be divided. It can be a character, a substring, or a regular expression. If the separator is not specified, the entire string is returned as a single array element. If the `separator` is not found in the string, the whole string is also returned as a single element in the array.
What is the purpose of the limit parameter in the split() method?
The limit parameter in the split() method defines the maximum number of splits to be performed on the string. Once the limit is reached, any remaining part of the string is not included in the resulting array. This allows developers to control the number of substrings created, which can be useful when only a certain number of segments are needed.
What is the return value of the split() method, and how is it structured?
The split() method returns an array of strings. Each element of this array is a substring of the original string, divided at each point where the specified separator occurs. The structure of the array depends on the occurrences of the separator and the specified limit.
How does the split() method handle cases where the separator is not found in the string?
If the specified separator is not found within the string, the split() method returns an array containing a single element, which is the entire original string. This ensures that the method always returns an array, regardless of whether any splits are made.
Please Login to comment...