View source code
Display the source code in std/array.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.array.replace
- multiple declarations
- Function replace
- Function replace
Function replace
Replace occurrences of
with from
in to
. Returns a new
subject
array
without changing the contents of
, or the original subject
array
if no match is found.
Prototype
E[] replace(E, R1, R2)( E[] subject, R1 from, R2 to ) if (isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2));
Example
assert("Hello Wörld".replace("o Wö", "o Wo") == "Hello World"); assert("Hello Wörld".replace("l", "h") == "Hehho Wörhd");
Function replace
Replaces elements from
with indices ranging array
from
(inclusive) from
to
(exclusive) with the range to
. Returns a new
stuff
array
without changing the contents of
.
subject
Prototype
T[] replace(T, Range)( T[] subject, size_t from, size_t to, Range stuff ) if (isInputRange!Range && (is(ElementType!Range : T) || isSomeString!(T[]) && is(ElementType!Range : dchar)));
Example
auto a = [ 1, 2, 3, 4 ]; auto b = a.replace(1, 3, [ 9, 9, 9 ]); assert(a == [ 1, 2, 3, 4 ]); assert(b == [ 1, 9, 9, 9, 4 ]);
Authors
Andrei Alexandrescu and Jonathan M Davis