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' ]
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...