Home >Web Front-end >JS Tutorial >JavaScript split() usage methods and examples_Basic knowledge

JavaScript split() usage methods and examples_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:40:291063browse

split definition and usage
The split() method is used to split a string into a string array.
Syntax
stringObject.split(separator, howmany) Parameter Description
separator Required. A string or regular expression to split the stringObject from where specified by this parameter.
howmany 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.
Return value
A string array. The array is created by splitting the string stringObject into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.
However, if separator is a regular expression containing subexpressions, then the returned array includes strings matching those subexpressions (but not text matching the entire regular expression).
Tips and Notes
Note: If an empty string ("") is used as a separator, each character in the stringObject will be split between them.
Note: The operation performed by String.split() is the opposite of the operation performed by Array.join.
In this example, we will split the string in different ways:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Output: The code is as follows:


How,are,you,doing,today?
H,o ,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?


How ,are,you example
In this example, we will split a string with a more complex structure:
"2:3:4:5".split(":") //will return ["2 ", "3", "4", "5"]
"|a|b|c".split("|") //will return ["", "a", "b", "c ", ""]
Example 3
Use the following code to split sentences into words:
var words = sentence.split(' ') or use regular expressions as separators:
var words = sentence.split(/s /) Example 4
If you want to split a word into letters, or a string into characters, you can use the following code:
"hello".split("") //Can return ["h", "e", "l", "l", "o"] If you only need to return a part of the characters, please use the howmany parameter:
"hello".split("", 3 ) //Can return ["h", "e", "l"]

There is not much else to say about the usage of js split. Let me give you an example directly below

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
The output result is Copy code
The code is as follows:


2
2
3
5
6
6
js split is to split a string into multiple strings with specific characters. You should understand it at a glance.
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