Home >Web Front-end >JS Tutorial >What is the use of JavaScript's split() method?
The split() method is used to split a string into a string array and return it. Syntax "string.split(separator,limit)", where the separator parameter specifies the position where the string is split. If the value is an empty string (""), the string will be split between each character.
The split() method is used to split a string into an array of strings.
Note: The split() method does not change the original string.
Syntax:
string.split(separator,limit)
Parameters | Description |
---|---|
separator | Optional. A string or regular expression to split the string Object from where specified by this parameter. |
limit | Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length. |
Tip: If an empty string ("") is used as a separator, each character in the stringObject will be split.
Return value:
A string array. The array is created by splitting the string string Object into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.
Example:
Split a string into a string array:
var str="Hello World !"; var n=str.split(" "); console.log(n);
Split Each character, including spaces
var str="Hello World !"; var n=str.split(""); console.log(n);
Use one character as delimiter
var str="Hello World !"; var n=str.split("o"); console.log(n);
Use limit parameter
var str="Hello World !"; var n=str.split(" ",2); console.log(n);
Recommended tutorial: "JavaScript Video Tutorial"
The above is the detailed content of What is the use of JavaScript's split() method?. For more information, please follow other related articles on the PHP Chinese website!