Should String.prototype.split accept variable arguments optionally?
Benjamin (Inglor) Gruenbaum
inglor at gmail.com
Wed Oct 16 05:54:14 PDT 2013
Splitting by one value or another seems to be a pretty common use case if
Stack Overflow questions and personal experience are an indication. For
example "-" and " " and "/".
Currently, the solution is to pass a regular expression to
However, it would be nice to be able to pass variable arguments to the
So if I want to split strings like "0-12-12" and "321+32/14" or "TI-CK ER"
instead of having to do:
myString.split(/ |-|\/|\+/g); // this is no fun to read
myString.split(" ","-","/","+"); // this is easier
myString.split([" ","-","/","+"]); // this is also easier.
The advantage of the second one (accepting an Array) is that it does not
require additional handling of the second "limit" parameter.
A possible advantage of the first one is that it's simpler.
The algorithm for the second one could be quite simple, in 188.8.131.52 only
_SplitMatch_ needs to be updated, and implementation sounds pretty simple
in engines and the semantics simple in the spec.
Some other languages:
- C# accepts an array of `char` or array of "string" in its Split,
- Ruby doesn't do this with `.split`, behaves like JS
- Java String.split only accepts a regex
- Python doesn't do this with `.split`
- Nice api addition that solves a common use case.
- A way to split by multiple delimiters easily without knowledge of
- Does not break the existing API, especially if we use array syntax.
- Extra overhead
- Use case needs assertion
(Reminds me of stuff in string_extras
What do you think?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss