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.

std.string.translate - multiple declarations

Function translate

Replaces the characters in str which are keys in transTable with their corresponding values in transTable. transTable is an AA where its keys are dchar and its values are either dchar or some type of string. Also, if toRemove is given, the characters in it are removed from str prior to translation. str itself is unaltered. A copy with the changes is returned.

C1[] translate(C1, C2) (
  C1[] str,
  in dchar[dchar] transTable,
  const(C2)[] toRemove = null
) pure @safe
if (isSomeChar!C1 && isSomeChar!C2);

C1[] translate(C1, S, C2) (
  C1[] str,
  in S[dchar] transTable,
  const(C2)[] toRemove = null
) pure @safe
if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2);

See Also

tr, replace, substitute

Parameters

NameDescription
str The original string.
transTable The AA indicating which characters to replace and what to replace them with.
toRemove The characters to remove from the string.

Example

dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q'];
writeln(translate("hello world", transTable1)); // "h5ll7 w7rld"

writeln(translate("hello world", transTable1, "low")); // "h5 rd"

string[dchar] transTable2 = ['e' : "5", 'o' : "orange"];
writeln(translate("hello world", transTable2)); // "h5llorange worangerld"

Function translate

This is an overload of translate which takes an existing buffer to write the contents to.

void translate(C1, C2, Buffer) (
  const(C1)[] str,
  in dchar[dchar] transTable,
  const(C2)[] toRemove,
  Buffer buffer
)
if (isSomeChar!C1 && isSomeChar!C2 && isOutputRange!(Buffer, C1));

void translate(C1, S, C2, Buffer) (
  C1[] str,
  in S[dchar] transTable,
  const(C2)[] toRemove,
  Buffer buffer
)
if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2 && isOutputRange!(Buffer, S));

Parameters

NameDescription
str The original string.
transTable The AA indicating which characters to replace and what to replace them with.
toRemove The characters to remove from the string.
buffer An output range to write the contents to.

Example

import std.array : appender;
dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q'];
auto buffer = appender!(dchar[])();
translate("hello world", transTable1, null, buffer);
writeln(buffer.data); // "h5ll7 w7rld"

buffer.clear();
translate("hello world", transTable1, "low", buffer);
writeln(buffer.data); // "h5 rd"

buffer.clear();
string[dchar] transTable2 = ['e' : "5", 'o' : "orange"];
translate("hello world", transTable2, null, buffer);
writeln(buffer.data); // "h5llorange worangerld"

Function translate

This is an ASCII-only overload of translate. It will not work with Unicode. It exists as an optimization for the cases where Unicode processing is not necessary.

C[] translate(C) (
  scope const(char)[] str,
  scope const(char)[] transTable,
  scope const(char)[] toRemove = null
) pure nothrow @trusted
if (is(immutable(C) == immutable(char)));

Unlike the other overloads of translate, this one does not take an AA. Rather, it takes a string generated by makeTransTable.

The array generated by makeTransTable is 256 elements long such that the index is equal to the ASCII character being replaced and the value is equal to the character that it's being replaced with. Note that translate does not decode any of the characters, so you can actually pass it Extended ASCII characters if you want to (ASCII only actually uses 128 characters), but be warned that Extended ASCII characters are not valid Unicode and therefore will result in a UTFException being thrown from most other Phobos functions.

Also, because no decoding occurs, it is possible to use this overload to translate ASCII characters within a proper UTF-8 string without altering the other, non-ASCII characters. It's replacing any code unit greater than 127 with another code unit or replacing any code unit with another code unit greater than 127 which will cause UTF validation issues.

See Also

tr, replace, substitute

Parameters

NameDescription
str The original string.
transTable The string indicating which characters to replace and what to replace them with. It is generated by makeTransTable.
toRemove The characters to remove from the string.

Example

auto transTable1 = makeTrans("eo5", "57q");
writeln(translate("hello world", transTable1)); // "h5ll7 w7rld"

writeln(translate("hello world", transTable1, "low")); // "h5 rd"

Function translate

This is an ASCII-only overload of translate which takes an existing buffer to write the contents to.

void translate(C, Buffer) (
  scope const(char)[] str,
  scope const(char)[] transTable,
  scope const(char)[] toRemove,
  Buffer buffer
) pure @trusted
if (is(immutable(C) == immutable(char)) && isOutputRange!(Buffer, char));

Parameters

NameDescription
str The original string.
transTable The string indicating which characters to replace and what to replace them with. It is generated by makeTransTable.
toRemove The characters to remove from the string.
buffer An output range to write the contents to.

Example

import std.array : appender;
auto buffer = appender!(char[])();
auto transTable1 = makeTransTable("eo5", "57q");
translate("hello world", transTable1, null, buffer);
writeln(buffer.data); // "h5ll7 w7rld"

buffer.clear();
translate("hello world", transTable1, "low", buffer);
writeln(buffer.data); // "h5 rd"

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis

License

Boost License 1.0.