JS Built-ins
← String128 / 195
StringString.prototype.splitintermediate

String.prototype.split() β€” split with limit

πŸ“– Usage Example
'a,b,c'.split(',')    // β†’ ['a', 'b', 'c']
'hello'.split('')     // β†’ ['h', 'e', 'l', 'l', 'o']
'hi'.split()          // β†’ ['hi']

Use split() to divide a string into an array of substrings at each occurrence of the separator.

String.prototype.split() β€” Limit

The optional second argument limits the number of substrings returned.

Challenge: Implement splitFirst(str, sep) that returns at most 2 parts.

splitFirst('a,b,c,d', ',') // β†’ ['a', 'b']
πŸ’‘ Hints (1)
  • β€’ `str.split(sep, 2)` returns an array with at most 2 elements.