The split() function is a string function of Node.js which is used to split strings into sub-strings. This function returns the output in array form.
Syntax:
string.split( separator )
Parameters: This function accepts a single parameter separator which holds the character to split the string.
Return Value: The function returns the split string and generates an array as result.
The below programs demonstrate the working of the split() function:
Example 1:
javascript
function splitStr(str) {
// Function to split string
let string = str.split( "*" );
console.log(string);
} // Initialize string const str = "Welcome*to*GeeksforGeeks" ;
// Function call splitStr(str); |
Output:
[ 'Welcome', 'to', 'GeeksforGeeks' ]
Example 2:
javascript
function splitStr(str, separator) {
// Function to split string
let string = str.split(separator);
console.log(string);
} // Initialize string const str = "GeeksforGeeks/A/computer/science/portal" ;
const separator = "/" ;
// Function call splitStr(str, separator); |
Output:
[ 'GeeksforGeeks', 'A', 'computer', 'science', 'portal' ]