Home  >  Article  >  Web Front-end  >  JavaScript Split() method_javascript skills

JavaScript Split() method_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:24:561216browse

Definition and usage of split() method:

The

split() method can use the substring of the string as a delimiter to split the string into a string array and return this array.

Note: The substring used as a delimiter will not become part of the elements of the returned array or a member of the array elements.

Here we only introduce the use of ordinary characters as separators. For information on using regular expressions as separators, please refer to the regular expression split() function chapter.

Click to see more related String object methods and properties.

Grammar structure:

Copy code The code is as follows:

stringObject.split(separator,limit)

Parameter list:

参数 描述
separator 必需。用于分割字符串的子字符串。
:如果此参数为一个空字符串"",那么stringObject中的每个字符都会被分割
limit 可选。设定字符串被分割的次数,如果省略此参数,则不限制分割次数。

Note: If this parameter is an empty string "", then each character in stringObject will be split

limit is optional. Set the number of times the string is divided. If this parameter is omitted, there is no limit to the number of times the string is divided.

Example code:

Copy code The code is as follows:

var a="a,b,c,d,e";
console.log(a.split(","));

Use the substring "," as the delimiter to split the string. This delimiter will not become part of the array element or a member of the array element. Output results: a, b, c, d, e.

Copy code The code is as follows:

var a="a,b,c,d,e";
console.log(a.split(",",2));

Limits the number of string splits, which also limits the dimensions of the returned array. Output result: 2.

Copy code The code is as follows:

var a="a,b,c,d,e";
console.log(a.split(""));

Use an empty string as the delimiter, then each character in the string will be split. Output results: a,,,b,,,c,,,d,,,e.

The above content is the relevant knowledge of the JavaScript Split() method introduced by the editor. I hope you like it.

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