String.prototype.split() | Js split space
Whenthestringisemptyandanon-emptyseparatorisspecified,split()returns[""].Ifthestringandseparatorarebothemptystrings,anemptyarrayisreturned.constemptyString="";console.log(emptyString.split("a"));console.log(emptyString.split(emptyString));Thefollowingexampledefinesafunctionthatsplitsastringintoanarrayofstringsusingseparator.Aftersplittingthestring,thefunctionlogsmessagesindicatingtheoriginalstring(beforethesplit),theseparatorused,thenumberofelementsinthearray,andtheindividualarrayelements.fu...
When the string is empty and a non-empty separator is specified, split() returns [""]. If the string and separator are both empty strings, an empty array is returned.
const emptyString = ""; console.log(emptyString.split("a")); console.log(emptyString.split(emptyString));The following example defines a function that splits a string into an array of strings using separator. After splitting the string, the function logs messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.
function splitString(stringToSplit, separator) { const arrayOfStrings = stringToSplit.split(separator); console.log("The original string is:", stringToSplit); console.log("The separator is:", separator); console.log( "The array has", arrayOfStrings.length, "elements:", arrayOfStrings.join(" / "), ); } const tempestString = "Oh brave new world that has such people ...