replace,
substitute
Parameters
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. |
C1[] translate(C1, C2 = immutable char)(C1[] str,
in dchar[dchar] transTable,
const(C2)[] toRemove = null) if (isSomeChar!C1 && isSomeChar!C2) @safe pureReplaces 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.
replace,
substitute
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. |
C1[] translate(C1, S, C2 = immutable char)(C1[] str,
in S[dchar] transTable,
const(C2)[] toRemove = null) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2) @safe pureDitto
void 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.
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. |
void 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
C[] 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 nothrowThis 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.
replace,
substitute
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. |
void 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 pureThis is an ASCII-only overload of translate which takes an existing buffer to write the contents to.
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. |