I am currently available for freelance/contract work. Book a meeting so we can talk about your project.

Splitting strings

Posted on

When we split a string using a sequence of characters, the string we split at no longer appears in the result.

If we split at every empty string, we get the individual characters, including spaces.

// If we split at “space”, no spaces appear in the result:
"two words".split(" ")           // ⇒ ["two", "words"]
 
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
 
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r")         // ⇒ ["hello wo", "ld"]
 
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("")              // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
// If we split at “space”, no spaces appear in the result:
"two words".split(" ")           // ⇒ ["two", "words"]
 
// Periods and other characters still remain:
"This is a sentence.".split(" ") // ⇒ ["This", "is", "a", "sentence."]
 
// If we split at a letter, that letter is removed. The space remains:
"hello world".split("r")         // ⇒ ["hello wo", "ld"]
 
// If we split at “empty space”, the string is split into all of its
// characters. The space is still in there:
"oh yeah".split("")              // ⇒ ["o", "h", " ", "y", "e", "a", "h"]
Debug
none
Grid overlay