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.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.
Range chomp(Range)
(
Range str
)
if ((isRandomAccessRange!Range && isSomeChar!(ElementEncodingType!Range) || isNarrowString!Range) && !isConvertibleToString!Range);
Range chomp(Range, C2)
(
Range str,
const(C2)[] delimiter
)
if ((isBidirectionalRange!Range && isSomeChar!(ElementEncodingType!Range) || isNarrowString!Range) && !isConvertibleToString!Range && isSomeChar!C2);
If no delimiter
is given, then one trailing '\r'
, '\n'
,
"\r\n"
, '\f'
, '\v'
, lineSep
, paraSep
, or nelSep
is removed from the end of str
. If str
does not end with any of those characters,
then it is returned unchanged.
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 .uni : lineSep, paraSep, nelSep;
import std .utf : decode;
writeln(chomp(" hello world \n\r")); // " hello world \n"
writeln(chomp(" hello world \r\n")); // " hello world "
writeln(chomp(" hello world \f")); // " hello world "
writeln(chomp(" hello world \v")); // " hello world "
writeln(chomp(" hello world \n\n")); // " hello world \n"
writeln(chomp(" hello world \n\n ")); // " hello world \n\n "
writeln(chomp(" hello world \n\n" ~ [lineSep])); // " hello world \n\n"
writeln(chomp(" hello world \n\n" ~ [paraSep])); // " hello world \n\n"
writeln(chomp(" hello world \n\n" ~ [nelSep])); // " hello world \n\n"
writeln(chomp(" hello world")); // " hello world"
writeln(chomp("")); // ""
writeln(chomp(" hello world", "orld")); // " hello w"
writeln(chomp(" hello world", " he")); // " hello world"
writeln(chomp("", "hello")); // ""
// Don't decode pointlessly
writeln(chomp("hello\xFE", "\r")); // "hello\xFE"
Authors
Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.