Polyfill of String.split()
const Split = (string, delimiter) => {
const output = [];
if (delimiter === "") return Array.from(string);
function startSplitting(str, index) {
if (index >= string.length) return;
let delimiterIndex = str.indexOf(delimiter);
if (delimiterIndex >= 0) {
output.push(str.substring(0, delimiterIndex));
startSplitting(str.substring(delimiterIndex + delimiter.length), 0);
} else {
output.push(str);
}
}
startSplitting(string, 0);
return output;
};
console.log(Split("The bus the ran too fast, the bus is gone.", "the"));
console.log(Split("ARJUNANK", ""));
console.log(Split("AR JU NAN K", " "));
// Output:
[ 'The bus ', ' ran too fast, ', ' bus is gone.' ]
["A", "R", "J", "U", "N", "A", "N", "K"];
[ 'AR', 'JU', 'NAN', 'K' ]