The split () method is used to split a string into an array of strings.
Example 1
In this case, we'll split the string in different ways:
var str= "How are your doing today?"
document.write (Str.split ("") + "
")
document.write (Str.split ("") + "
")
document.write (Str.split ("" ", 3)
//output:
//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 2
In this case, we'll split the more complex string of structures:
"2:3:4:5". Split (":")//will return ["2", "3", "4", "5"]
"|a|b|c". Split ("|")//will return ["", "a", "B", "C"]
Example 3
Using the following code, you can split a sentence into words:
var words = Sentence.split (')
//or using regular expressions as separator:
var words = Sentence.split (/\s+/)
Example 4
If you want to split the word into letters, or split the string into characters, use the following code:
"Hello". Split ("")//May return ["H", "E", "L", "L", "O"]
//If you only need to return part of the character, use the Howmany parameter:
"Hello". Split ("", 3)//May return ["H", " E "," L "]
Instance:
The above mentioned is the entire content of this article, I hope you can enjoy.