View source code
Display the source code in std/array.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.

Function std.array.replaceLast

Replaces the last occurrence of from with to in subject.

E[] replaceLast(E, R1, R2) (
  E[] subject,
  R1 from,
  R2 to
if (isDynamicArray!(E[]) && isForwardRange!R1 && is(typeof(appender!(E[])().put(from[0..1]))) && isForwardRange!R2 && is(typeof(appender!(E[])().put(to[0..1]))));


subject the array to scan
from the item to replace
to the item to replace from with


A new array without changing the contents of subject, or the original array if no match is found.


auto a = [1, 2, 2, 3, 4, 5];
auto b = a.replaceLast([2], [1337]);
writeln(b); // [1, 2, 1337, 3, 4, 5]

auto s = "This is a foo foo list";
auto r = s.replaceLast("foo", "silly");
writeln(r); // "This is a foo silly list"


Andrei Alexandrescu and Jonathan M Davis


Boost License 1.0.