Home >Web Front-end >JS Tutorial >How to use js split method
Usage of js split method: The split method is used to split a string into a string array. The syntax is [stringObject.split(separator,howmany)], where the separator parameter is required and the howmany parameter is optional. select.
Related learning recommendations: javascript video tutorial
split() method is processed in js String is a very common and important method that must be mastered.
<strong>split()</strong>
method is used to split a string into characters string array.
Syntax
stringObject.split(separator,howmany)
Parameter description
separator
Parameters: required. A string or regular expression to split the stringObject from where specified by this parameter.
howmany
Parameters: 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.
For specific usage, see the following code
1,
<script type="text/javascript" charset="utf-8"> var str = "I lover you too"; var aa = str.split(" "); console.log(aa); var bb = str.split(""); console.log(bb); //返回["I", " ", "l", "o", "v", "e", "r", " ", "y", "o", "u", " ", "t", "o", "o"] //不传任何切割标志时,默认切割每一个字符 var cc = str.split(" ",2); console.log(cc); //返回["I", "lover"],以空格切割,返回前两个数组元素 </script>
2,
"2:3:4:5".split(":") //将返回["2", "3", "4", "5"] "|a|b|c".split("|") //将返回["", "a", "b", "c"]
3,
"hello".split("") //可返回 ["h", "e", "l", "l", "o"],分割单词 "hello".split("", 3) //可返回 ["h", "e", "l"],取单词前三个字母
The above is the detailed content of How to use js split method. For more information, please follow other related articles on the PHP Chinese website!