Home / Docs / Standard library / Prelude
replace
A function of the prelude, in scope in every module. Edition 0.2 and later.
pub replace(t: Text, pattern: Text, by: Text) -> Text
requires pattern.len > 0t with every non-overlapping occurrence of pattern replaced by by, left to right (spec/22 §2).
Parameters
t: the textpattern: the text to replaceby: 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
Cost
Time O(b + m + result) (ADR 0070), where:
b: the length oftin UTF-8 bytesm: the length ofpatternin UTF-8 bytesresult: the length of the result in UTF-8 bytes
Example
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:
Crème brûlée, large, boxedSpecification
Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.