Skip to curriculum
Jump to the editor

2.3. String.prototype.split

Medium
Topics

This question is in

2. Strings and text
15–30 min

split is behind almost every bit of text parsing in the browser: reading a CSV line, a query string, a path, a list of tags typed with commas. Its rules for the odd cases are precise and surprising: "".split(",") is [""] but "".split("") is [], a separator at the end leaves an empty piece, and a limit of -1 means "no limit". Rebuilding it shows each of those rules.

Write split(text, separator, limit) in split.js. It behaves like text.split(separator, limit) when separator is not a regular expression.

Signature

JAVASCRIPT
function split(text, separator, limit) // an array of strings
  • text: the value to split, converted to a string.
  • separator: where to cut, converted to a string. Not a regular expression.
  • limit (optional): the most pieces to return.

Requirements

  1. Returns the pieces of text between occurrences of separator, in order. A separator of several characters works, and is plain text: ., * and + have no special meaning.
  2. Empty pieces are kept: before a separator at the start, after one at the end, and between two separators next to each other.
  3. Matches do not overlap: after a match, the search continues from its end. split("aaa", "aa") is ["", "a"].
  4. When the separator is not found, the result is [text]; an empty text gives [""].
  5. An empty separator splits text into its UTF-16 code units, as the built-in does (an emoji gives two pieces); an empty text then gives [].
  6. An undefined separator (or none) returns [text].
  7. limit is the most pieces to return. undefined means no limit. Any other value is converted the way the built-in converts it: to an unsigned 32-bit integer, so 2.7 is 2, "2" is 2, -1 is 4294967295, and NaN and null are 0.
  8. A limit of 0 returns [], whatever the separator.
  9. separator and text are converted with String: split(12321, 2) is ["1", "3", "1"].
  10. Every call returns a new array, and a text of 20,000 pieces is split quickly.

Examples

JAVASCRIPT
split("a,b,c", ",");      // ["a", "b", "c"]
split(",a,,b,", ",");     // ["", "a", "", "b", ""]
split("aaa", "aa");       // ["", "a"]
split("abc", "x");        // ["abc"]
JAVASCRIPT
split("abc", "");         // ["a", "b", "c"]
split("", "");            // []
split("", ",");           // [""]
split("a,b");             // ["a,b"]
JAVASCRIPT
split("a,b,c,d", ",", 2); // ["a", "b"]
split("a,b,c", ",", -1);  // ["a", "b", "c"]
split("a,b,c", ",", 0);   // []

Environment

  • Plain JavaScript in a browser page; the tests call the global split from split.js.
  • Not allowed: String.prototype.split. The tests replace it with a function that throws. indexOf and slice may be used.
  • Regular-expression separators are not part of this problem.

Hints

  1. indexOf takes a second argument. What is it for?
  2. How do you convert a number to an unsigned 32-bit integer with one operator?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…