split function can be used to split a string and return it as a new array, we will use split function to separate the array by space character, the space character we mentioned will be our separator which is used by split function How to decompose a string.
Basic syntax of the split function
string.split([separator][, limit]);
separator indicates the character specified to separate the string. If the separator is omitted, the returned array contains one element consisting of the entire string.
limit indicates 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.
Additionally, when the string is empty, split returns an array containing an empty string instead of an empty array.
Given an empty argument, split() will create a comma-separated array for each character in the string.
Let’s look at a specific example
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> const originalString = "How are you?"; const splitString = originalString.split(" "); console.log(splitString); </script> </body> </html>
The effect is as follows
This article ends here That’s all over. For more exciting content, you can pay attention to the relevant column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to use split function. For more information, please follow other related articles on the PHP Chinese website!