Example of how to use the string split function in JavaScript
Let's take a look at the following code:
1 2 3 4 5 6 |
<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> |
The output result is as follows:
1 2 3 |
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:
1 2 |
"2: 3: 4: 5". split (":") // Returns ["2", "3", "4", "5"] "| A | B | c". split ("|") // ["", "a", "B", "c"] |
Use the following code to split sentences into words:
1 |
Var words = sentence. split ('') |
If you want to split words into letters or strings into characters, use the following code:
1 |
"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:
1 |
"Hello". split ("", 3) // you can return ["h", "e", "l"] |
Or use the regular expression as the separator:
1 |
Var words = sentence. split (/\ s + /) |