The split method spilt the string into array of substring based on the pattern provided.
let numbersStr = "1,2,3,4,5"; let numArray = numbersStr.split(","); console.log(numArray); // ["1","2","3","4","5"]
In the above example , the split method will loop through all the character of the string , if the pattern provided is found then the split method will collect the characters looped before finding the pattern and join all the characters as a string and pushed that string into an array .
If we don’t pass any pattern then the entire string will be pushed to an array and returned
numbersStr.split(); // ["1,2,3,4,5"];
If the pattern passed is the first character of the string , then an empty stirng is pushed into the array .
numbersStr.split("1"); // ["", "2,3,4,5"]
Also if the pattern passed is the last character if string , then an empty string is pushed to the resultant array.
numbersStr.split("5")' // ["1,2,3,4", ""]
If we pass pattern as empty string the each character of the string is splitted and pushed into array
numbersStr.split(""); // ["1", ",", "2", ",", "3", ",", "4", ",", "5"]
📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=09BwruU4kiY&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=6
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
The syntax is following for String Split Example.
The first argument to the split function is the string which specifies the points where the split has to take place.
This argument can be treated as a simple string or as a regular expression. If the separator is unspecified, then the whole string becomes a single array element.
The same thing also happens when a separator is not present in the string. If the separator is an empty string (“”), then every character of the string is separated.
The second argument to a function limit defines an upper limit on the number of splits to be found in a given string. If a string remains unchecked after a threshold or limit is reached, then it is not reported in an array.
If an empty string (”) is used as the separator, the string is split between each character. The string split() method does not change an original string. The syntax for the split() method is the following.
The separator is an optional parameter. The separator specifies a character, or a regular expression, to use for splitting a string. If omitted, then the entire string will be returned.
The limit is an optional parameter. It is an integer that specifies the number of splits, items after the split limit will not be included in the array.