Return javascript string object reference manual
Definition and usage
The split () method is used to split a string into a string array.
Syntax
Stringobject. Split (separator, howator)
Parameter description
Separator is required. String or regular expression, which separates stringobject from the specified place.
Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.
Return Value
A string array. This array is created by dividing the string stringobject into substrings at the boundary specified by separator. The strings in the returned array do not include the separator itself.
However, if separator is a regular expression that contains a subexpression, the returned array contains the strings that match the subexpression (but does not include the text that matches the entire regular expression ).
Tips and comments
NOTE: If an empty string ("") is used as a separator, each character in the stringobject is separated.
Note: The operations executed by string. Split () are the opposite to those executed by array. Join.
Instance
Example 1
In this example, we will split the string in different ways:
<SCRIPT type = "text/JavaScript"> var STR = "How are you doing today? "Document. write (Str. split ("") + "<br/>") document. write (Str. split ("") + "<br/>") document. write (Str. split ("", 3) </SCRIPT>
Output:
How, are, you, doing, today? H, O, W, A, R, E, Y, O, U, D, o, I, n, G, T, O, D,, Y ,? How, are, you
Example 2
In this example, we split strings with more complex structures:
"2: 3: 4: 5 ". split (":") // Returns ["2", "3", "4", "5"] "| A | B | C ". split ("|") // Returns ["", "A", "B", "C"]
Example 3
Use the following code to split sentences into words:
VaR words = sentence. Split ('')
Or use the regular expression as the separator:
VaR words = sentence. Split (/\ s + /)
Example 4
If you want to split words into letters or strings into characters, use the following code:
"Hello". Split ("") // you can return ["H", "E", "L", "L", "O"]
If you only need to return a part of the characters, use the howmany parameter:
"Hello". Split ("", 3) // you can return ["H", "E", "L"]
Tiy
Split ()
How to use split () to separate strings.