Sorry, I should really run things in an IDE before posting code examples and results!
For examples ":".split(":", 0) and "".split(":", 0), they actually produce [] and [""] respectively (which I still argue is inconsistent and undesired for the proposed String.splits()). For examples ":".split(":", -1) and "".split(":", -1), they actually produce ["", ""] and [""] respectively, which I like better. Cheers, Jonathan On 14 March 2018 at 00:12, Jonathan Bluett-Duncan <jbluettdun...@gmail.com> wrote: > Paul, > > AFAICT, one sort of behaviour which String.split() allows which > Pattern.splitAsStream() and the proposed String.splits() don't is allowing > a negative limit, e.g. String.split(string, -1). > > Over at http://errorprone.info/bugpattern/StringSplitter, they argue that > a limit of -1 has less surprising behaviour than the default of 0, because > e.g. "".split(":") produces [] (empty array), whereas ":".split(":") > produces [""] (array with an empty string), which IMO is not consistent. > > This compares with ":".split(":", -1) and "".split(":", -1) which produce > ["", ""] (array with two empty strings, each representing ends of `:`) and > [] (empty array) respectively - more consistent IMO. > > Should String.splits(`\n|\r\n?`) follow the behaviour of String.split(..., > 0) or String.split(..., -1)? I'd personally argue for the latter. > > Cheers, > Jonathan > > On 13 March 2018 at 23:22, Paul Sandoz <paul.san...@oracle.com> wrote: > >> >> >> > On Mar 13, 2018, at 3:49 PM, John Rose <john.r.r...@oracle.com> wrote: >> > >> > On Mar 13, 2018, at 6:47 AM, Jim Laskey <james.las...@oracle.com> >> wrote: >> >> >> >> … >> >> A. Line support. >> >> >> >> public Stream<String> lines() >> >> >> > >> > Suggest factoring this as: >> > >> > public Stream<String> splits(String regex) { } >> >> +1 >> >> This is a natural companion to the existing array returning method (as it >> was the case on Pattern when we added splitAsStream), where one can use a >> limit() operation to achieve the same effect as the limit parameter on the >> array returning method. >> >> >> > public Stream<String> lines() { return splits(`\n|\r\n?`); } >> > >> >> See also Files/BufferedReader.lines. (Without going into details >> Files.lines has some interesting optimizations.) >> >> Paul. > > >