replace

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

  1. E[] replace(E[] subject, R1 from, R2 to)
  2. E[] replace(E[] subject, R1 from, R2 to, size_t changed)
    E[]
    replace
    (
    E
    R1
    R2
    )
    (,
    R1 from
    ,
    R2 to
    ,
    ref size_t changed
    )
    if (
    is(Unqual!E : Unqual!R1)
    )
  3. T[] replace(T[] subject, size_t from, size_t to, Range stuff)

Parameters

subject E[]

the array to scan

from R1

the item to replace

to R2

the item to replace all instances of from with

changed size_t

the number of replacements

Return Value

Type: E[]

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

Examples

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

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

Meta