---
title: "replace"
url: https://una-lang.org/en/docs/std/prelude/replace/
release: v0.4.0
edition: "0.2 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/listing.json
---

# replace

A function of the prelude, in scope in every module. Edition 0.2 and later.

```una
pub replace(t: Text, pattern: Text, by: Text) -> Text
  requires pattern.len > 0
```

`t` with every non-overlapping occurrence of `pattern` replaced by `by`, left to right (`spec/22 §2`).

## Parameters

- `t`: the text
- `pattern`: the text to replace
- `by`: the text that takes its place

## Returns

`Text`: `t` with every non\-overlapping occurrence of `pattern` replaced by `by`\, left to right.

## Requires

- `pattern.len > 0`: the pattern is not empty

Each condition under `requires` is an obligation at every call\, which `una check` proves \(`spec/12 §3`\)\: it reports U5001 when the condition is refuted and U5002 when it is unknown \(`spec/12 §6`\)\, and `una run`\, `una test` and `una build` refuse a package with an obligation that is not proved \(`spec/22 §2`\)\.

## See also

[`split`](https://una-lang.org/en/docs/std/prelude/split/), [`contains_text`](https://una-lang.org/en/docs/std/prelude/contains_text/)

## Cost

Time `O(b + m + result)` (ADR 0070), where:

- `b`: the length of `t` in UTF\-8 bytes
- `m`: the length of `pattern` in UTF\-8 bytes
- `result`: the length of the result in UTF\-8 bytes

## Example

`src/shop.una`

```una
module shop

--- A product title with its separator changed.
pub main() -> Unit
  uses @console: Console[print]
=
  @console.print("Crème brûlée - large - boxed".replace(pattern: " - ", by: ", "))
```

`una run .` prints:

```text
Crème brûlée, large, boxed
```

## Specification

- [`spec/22 §2`](https://una-lang.org/en/docs/spec/22-text/#s2)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
