Home > Web Front-end > JS Tutorial > body text

How to use js split method

coldplay.xixi
Release: 2020-09-11 14:50:17
Original
10500 people have browsed it

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.

How to use js split method

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)
Copy after login

Parameter description

  • separator Parameters: required. A string or regular expression to split the stringObject from where specified by this parameter.

  • howmanyParameters: 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>
Copy after login

2,

"2:3:4:5".split(":")    
//将返回["2", "3", "4", "5"]
"|a|b|c".split("|") 
//将返回["", "a", "b", "c"]
Copy after login

3,

"hello".split("")   
//可返回 ["h", "e", "l", "l", "o"],分割单词
"hello".split("", 3)    
//可返回 ["h", "e", "l"],取单词前三个字母
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template