String line to split.
Separator character with which to split the line.
Maximum length of the separated lines.
A T[] array with lines split out of the passed line.
string line = "I am a fish in a sort of long sentence~"; enum maxLineLength = 20; auto splitLines = line.splitLineAtPosition(' ', maxLineLength); assert(splitLines[0] == "I am a fish in a"); assert(splitLines[1] == "sort of a long"); assert(splitLines[2] == "sentence~");
import std.conv : text; { immutable prelude = "PRIVMSG #garderoben :"; immutable maxLength = 250 - prelude.length; immutable rawLine = "Lorem ipsum dolor sit amet, ea has velit noluisse, " ~ "eos eius appetere constituto no, ad quas natum eos. Perpetua " ~ "electram mnesarchum usu ne, mei vero dolorem no. Ea quando scripta " ~ "quo, minim legendos ut vel. Ut usu graece equidem posidonium. Ius " ~ "denique ponderum verterem no, quo te mentitum officiis referrentur. " ~ "Sed an dolor iriure vocibus. " ~ "Lorem ipsum dolor sit amet, ea has velit noluisse, " ~ "eos eius appetere constituto no, ad quas natum eos. Perpetua " ~ "electram mnesarchum usu ne, mei vero dolorem no. Ea quando scripta " ~ "quo, minim legendos ut vel. Ut usu graece equidem posidonium. Ius " ~ "denique ponderum verterem no, quo te mentitum officiis referrentur. " ~ "Sed an dolor iriure vocibus. ssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "ssssssssssssssssssssssssssssssssssssssssssssssssssssssss"; const splitLines = rawLine.splitLineAtPosition(' ', maxLength); assert((splitLines.length == 4), splitLines.length.text); } { immutable prelude = "PRIVMSG #garderoben :"; immutable maxLength = 250 - prelude.length; immutable rawLine = "ssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" ~ "ssssssssssssssssssssssssssssssssssssssssssssssssssssssss"; const splitLines = rawLine.splitLineAtPosition(' ', maxLength); assert((splitLines.length == 1), splitLines.length.text); assert(splitLines[0] == rawLine); }
Splits a string with on boundary as delimited by a supplied separator, into one or more more lines not longer than the passed maximum length.
If a line cannot be split due to the line being too short or the separator not occurring in the text, it is added to the returned array as-is and no more splitting is done.