The split() method can split a string into an array of substrings.
The split() method can split a string into an array of substrings.
If an empty string "" is used as the separator, the string is by each character.
The split() method does not change the original string.
string.split(separator,limit)
Parameter | Require | Description |
---|---|---|
separator | Optional. | character or the regular expression to split the string. |
limit | Optional. | An integer that specifies the number of splits, and items after the limit will not be included in the array |
An Array, containing the splitted values
Split a string into an array of substrings:
//display the array values after the split. var str = "this is a test test test ?"; var res = str.split(" "); console.log(res);//from w w w.ja va2 s . c o m //Omit the separator parameter: var str = "this is a test?"; var res = str.split(); console.log(res); //Separate each character, including white-space: var str = "How are you doing today?"; var res = str.split(""); console.log(res); //Use the limit parameter: var str = "How are you doing today?"; var res = str.split(" ", 3); console.log(res); //Use a letter as a separator: var str = "this is a test?"; var res = str.split("t"); console.log(res);