xxxxxxxxxx
const string = "this is the array that we want to split";
var splittedString = string.split(" ") //place the separator as a parameter
/* splittedString:
* ['this', 'is', 'the', 'array', 'that', 'we', 'want', 'to', 'split']
*/
xxxxxxxxxx
// bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550
var arr = foo.split('');
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
xxxxxxxxxx
var names = 'Harry ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';
console.log(names);
var re = /\s*(?:;|$)\s*/;
var nameList = names.split(re);
console.log(nameList);
xxxxxxxxxx
//The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
const str1 = 'The quick brown fox jumps over the lazy dog.';
const words = str1.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str1.split('');
console.log(chars[8]);
// expected output: "k"
const strCopy = str1.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]
xxxxxxxxxx
// The split() method breaks a string into an array of substrings
let text = "A plantain planter planted a plantain in a plantain plantation";
let output = text.split(/Plant/gi);
console.log(output); // ["A ","ain ","er ","ed a ","ain in a ","ain ","ation"]
xxxxxxxxxx
var myString = "Hello World!";
// splitWords is an array
// [Hello,World!]
var splitWords = myString.split(" ");
// e.g. you can use it as an index or remove a specific word:
var hello = splitWords[splitWords.indexOf("Hello")];
xxxxxxxxxx
const printText = "The quick brown fox jumps";
const printSplit = printText.split(" ")
console.log(printSplit);
//output:[ 'The', 'quick', 'brown', 'fox', 'jumps' ]
console.log(printSplit[4]);
//Output: jumps
xxxxxxxxxx
"Abebe beso bela".split(" "); // output ['Abebe', 'beso', 'bela'] means split the string when you find a space
"Abebe beso bela".split(" "); // output ['Abebe beso bela'] means split the string when you find two spaces
"Abebe beso bela".split(" "); // output ['Abebe', 'beso bela'] ,it found two spaces so splits the string.