View source code
Display the source code in std/string.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.string.lineSplitter

Split an array or slicable range of characters into a range of lines using '\r', '\n', '\v', '\f', "\r\n", lineSep, paraSep and '\u0085' (NEL) as delimiters. If keepTerm is set to Yes.keepTerminator, then the delimiter is included in the slices returned.

auto auto lineSplitter(Flag!("keepTerminator") keepTerm = No.keepTerminator, Range) (
  Range r
)
if (hasSlicing!Range && hasLength!Range && isSomeChar!(ElementType!Range) && !isSomeString!Range);

auto auto lineSplitter(Flag!("keepTerminator") keepTerm = No.keepTerminator, C) (
  C[] r
)
if (isSomeChar!C);

Does not throw on invalid UTF; such is simply passed unchanged to the output.

Adheres to Unicode 7.0.

Does not allocate memory.

Parameters

NameDescription
r array of chars, wchars, or dchars or a slicable range
keepTerm whether delimiter is included or not in the results

Returns

range of slices of the input range r

See Also

splitLines std.algorithm.splitter splitter

Example

import std.array : array;

string s = "Hello\nmy\rname\nis";

/* notice the call to 'array' to turn the lazy range created by
lineSplitter comparable to the string[] created by splitLines.
*/
writeln(lineSplitter(s).array); // splitLines(s)

Example

auto s = "\rpeter\n\rpaul\r\njerry\u2028ice\u2029cream\n\nsunday\nmon\u2030day\n";
auto lines = s.lineSplitter();
static immutable witness = ["", "peter", "", "paul", "jerry", "ice", "cream", "", "sunday", "mon\u2030day"];
uint i;
foreach (line; lines)
{
    writeln(line); // witness[i++]
}
writeln(i); // witness.length

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis

License

Boost License 1.0.