translate

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

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.

See Also

tr, replace, substitute

Parameters

strThe original string.
transTableThe AA indicating which characters to replace and what to replace them with.
toRemoveThe characters to remove from the string.
fnC1[] translate(C1, S, C2 = immutable char)(C1[] str, in S[dchar] transTable, const(C2)[] toRemove = null) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2) @safe pure

Ditto

fnvoid translate(C1, C2 = immutable char, Buffer)(const(C1)[] str, in dchar[dchar] transTable, const(C2)[] toRemove, Buffer buffer) if (isSomeChar!C1 && isSomeChar!C2 && isOutputRange!(Buffer, C1))

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

Parameters

strThe original string.
transTableThe AA indicating which characters to replace and what to replace them with.
toRemoveThe characters to remove from the string.
bufferAn output range to write the contents to.
fnvoid translate(C1, S, C2 = immutable char, Buffer)(C1[] str, in S[dchar] transTable, const(C2)[] toRemove, Buffer buffer) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2 && isOutputRange!(Buffer, S))

Ditto

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

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.

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

strThe original string.
transTableThe string indicating which characters to replace and what to replace them with. It is generated by makeTransTable.
toRemoveThe characters to remove from the string.
fnvoid translate(C = immutable char, Buffer)(scope const(char)[] str, scope const(char)[] transTable, scope const(char)[] toRemove, Buffer buffer) if (is(immutable C == immutable char) && isOutputRange!(Buffer, char)) @trusted pure

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

Parameters

strThe original string.
transTableThe string indicating which characters to replace and what to replace them with. It is generated by makeTransTable.
toRemoveThe characters to remove from the string.
bufferAn output range to write the contents to.