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.

std.array.replace - multiple declarations

Function replace

Replace occurrences of from with to in subject in a new array.

E[] replace(E, R1, R2) (
  E[] subject,
  R1 from,
  R2 to
)
if (isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2) || is(Unqual!E : Unqual!R1));

Parameters

NameDescription
subject the array to scan
from the item to replace
to the item to replace all instances of from with

Returns

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

See Also

substitute for a lazy replace.

Example

writeln("Hello Wörld".replace("o Wö", "o Wo")); // "Hello World"
writeln("Hello Wörld".replace("l", "h")); // "Hehho Wörhd"

Function replace

Replace occurrences of from with to in subject in a new array. changed counts how many replacements took place.

E[] replace(E, R1, R2) (
  E[] subject,
  R1 from,
  R2 to,
  ref size_t changed
)
if (isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2) || is(Unqual!E : Unqual!R1));

Parameters

NameDescription
subject the array to scan
from the item to replace
to the item to replace all instances of from with
changed the number of replacements

Returns

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

Example

size_t changed = 0;
writeln("Hello Wörld".replace("o Wö", "o Wo", changed)); // "Hello World"
writeln(changed); // 1

changed = 0;
writeln("Hello Wörld".replace("l", "h", changed)); // "Hehho Wörhd"
import std.stdio : writeln;
writeln(changed);
writeln(changed); // 3

Function replace

Replaces elements from array with indices ranging from from (inclusive) to to (exclusive) with the range stuff.

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)));

Parameters

NameDescription
subject the array to scan
from the starting index
to the ending index
stuff the items to replace in-between from and to

Returns

A new array without changing the contents of subject.

See Also

substitute for a lazy replace.

Example

auto a = [ 1, 2, 3, 4 ];
auto b = a.replace(1, 3, [ 9, 9, 9 ]);
writeln(a); // [1, 2, 3, 4]
writeln(b); // [1, 9, 9, 9, 4]

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.