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.

Function std.array.replaceFirst

Replaces the first occurrence of from with to in a. Returns a new array without changing the contents of subject, or the original array if no match is found.

Prototype

E[] replaceFirst(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]))));

Example

auto a = [1, 2, 2, 3, 4, 5];
auto b = a.replaceFirst([2], [1337]);
assert(b == [1, 1337, 2, 3, 4, 5]);

auto s = "This is a foo foo list";
auto r = s.replaceFirst("foo", "silly");
assert(r == "This is a silly foo list");

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.

Comments