chomp

If str ends with delimiter, then str is returned without delimiter on its end. If it str does not end with delimiter, then it is returned unchanged.

If no delimiter is given, then one trailing '\r', '\n', "\r\n", '\f', '\v', std.uni.lineSep, std.uni.paraSep, or std.uni.nelSep is removed from the end of str. If str does not end with any of those characters, then it is returned unchanged.

  1. Range chomp(Range str)
    Range
    chomp
    (
    Range
    )
    (
    Range str
    )
  2. Range chomp(Range str, const(C2)[] delimiter)

Parameters

str Range

string or indexable range of characters

Return Value

Type: Range

slice of str

Examples

import std.uni : lineSep, paraSep, nelSep;
import std.utf : decode;
assert(chomp(" hello world  \n\r") == " hello world  \n");
assert(chomp(" hello world  \r\n") == " hello world  ");
assert(chomp(" hello world  \f") == " hello world  ");
assert(chomp(" hello world  \v") == " hello world  ");
assert(chomp(" hello world  \n\n") == " hello world  \n");
assert(chomp(" hello world  \n\n ") == " hello world  \n\n ");
assert(chomp(" hello world  \n\n" ~ [lineSep]) == " hello world  \n\n");
assert(chomp(" hello world  \n\n" ~ [paraSep]) == " hello world  \n\n");
assert(chomp(" hello world  \n\n" ~ [ nelSep]) == " hello world  \n\n");
assert(chomp(" hello world ") == " hello world ");
assert(chomp(" hello world") == " hello world");
assert(chomp("") == "");

assert(chomp(" hello world", "orld") == " hello w");
assert(chomp(" hello world", " he") == " hello world");
assert(chomp("", "hello") == "");

// Don't decode pointlessly
assert(chomp("hello\xFE", "\r") == "hello\xFE");

Meta