Function std.string.tr
Replaces the characters in str
which are in from
with the
the corresponding characters in to
and returns the resulting string.
C1[] tr(C1, C2, C3, C4)
(
C1[] str,
const(C2)[] from,
const(C3)[] to,
const(C4)[] modifiers = null
);
tr
is based on
Posix's tr,
though it doesn't do everything that the Posix utility does.
Parameters
Name | Description |
---|---|
str | The original string. |
from | The characters to replace. |
to | The characters to replace with. |
modifiers | String containing modifiers. |
Modifiers
Modifier | Description |
'c' | Complement the list of characters in from |
'd' | Removes matching characters with no corresponding
replacement in to |
's' | Removes adjacent duplicates in the replaced characters |
If the modifier 'd'
is present, then the number of characters in
to
may be only 0
or 1
.
If the modifier 'd'
is not present, and to
is empty, then
to
is taken to be the same as from
.
If the modifier 'd'
is not present, and to
is shorter than
from
, then to
is extended by replicating the last character in
to
.
Both from
and to
may contain ranges using the '-'
character
(e.g. "a-d"
is synonymous with "abcd"
.) Neither accept a leading
'^'
as meaning the complement of the string (use the 'c'
modifier
for that).
See Also
Example
writeln(tr("abcdef", "cd", "CD")); // "abCDef"
writeln(tr("1st March, 2018", "March", "MAR", "s")); // "1st MAR, 2018"
writeln(tr("abcdef", "ef", "", "d")); // "abcd"
writeln(tr("14-Jul-87", "a-zA-Z", " ", "cs")); // " Jul "
Authors
Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis