View source code
Display the source code in std/string.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.string.chomp
If
ends with str
, then delimiter
is returned without
str
on its end. If it delimiter
does not end with
str
, then it is returned unchanged.
delimiter
If no
is given, then one trailing delimiter
'\r'
, '\n'
,
"\r\n"
, '\f'
, '\v'
, std.uni.lineSep
, std.uni.paraSep
, or std.uni.nelSep
is removed from the end of
. If str
does not end with any of those characters,
then it is returned unchanged.
str
Prototypes
Range chomp(Range)( Range str ) if (isRandomAccessRange!Range && isSomeChar!(ElementEncodingType!Range) || isSomeString!Range); Range chomp(Range, C2)( Range str, const(C2)[] delimiter ) if ((isBidirectionalRange!Range && isSomeChar!(ElementEncodingType!Range) || isSomeString!Range) && isSomeChar!C2);
Parameters
Name | Description |
---|---|
str | string or indexable range of characters |
delimiter | string of characters to be sliced off end of str [] |
Returns
slice of str
Example
import std.utf : decode; import std.uni : lineSep, paraSep, nelSep; 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("") == ""); 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");
Authors
Walter Bright, Andrei Alexandrescu, and Jonathan M Davis