Node.js split() function
Last Updated :
30 Mar, 2023
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) {
let string = str.split( "*" );
console.log(string);
}
const str = "Welcome*to*GeeksforGeeks" ;
splitStr(str);
|
Output:
[ 'Welcome', 'to', 'GeeksforGeeks' ]
Example 2:
javascript
function splitStr(str, separator) {
let string = str.split(separator);
console.log(string);
}
const str = "GeeksforGeeks/A/computer/science/portal" ;
const separator = "/" ;
splitStr(str, separator);
|
Output:
[ 'GeeksforGeeks', 'A', 'computer', 'science', 'portal' ]
Share your thoughts in the comments
Please Login to comment...