Home >Web Front-end >JS Tutorial >What is the use of JavaScript's split() method?

What is the use of JavaScript's split() method?

青灯夜游
青灯夜游Original
2020-07-28 11:11:004036browse

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.

What is the use of JavaScript's split() method?

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);

What is the use of JavaScripts split() method?

Split Each character, including spaces

var str="Hello World !";
var n=str.split("");
console.log(n);

What is the use of JavaScripts split() method?

Use one character as delimiter

var str="Hello World !";
var n=str.split("o");
console.log(n);

What is the use of JavaScripts split() method?

Use limit parameter

var str="Hello World !";
var n=str.split(" ",2);
console.log(n);

What is the use of JavaScripts split() method?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn