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

Replace occurrences of from with to in subject. Returns a new array without changing the contents of subject, or the original 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 array with indices ranging from from (inclusive) to to (exclusive) with the range stuff. Returns a new 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

License

Boost License 1.0.

Comments