replace
Builtin index · Collections & strings
result = replace(s, old, new, [count], [case=], [pattern=], [keep=])
Input: s, old, new, count, case, pattern, keep · Output: result
What it is
s is the Str to search; old and new are Strs. Every literal (non-regex) occurrence of old in s is replaced by new. Optional keep (a Str or a List of them, in the same pattern language as old) leaves alone any occurrence that falls inside that text: replace(s, "<", "<", keep="<br>").
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = replace(s, old, new, [count], [case=], [pattern=], [keep=]) | s Strs is the Str to search.old Strold and new are Strs.new Strold and new are Strs.count optionalnot documented yetcase optionalnot documented yetpattern Str or List, optionalOptional keep (a Str or a List of them, in the same pattern language as old) leaves alone any occurrence that falls inside that text: replace(s, "<", "<", keep="<br>").keep Str or List, optionalOptional keep (a Str or a List of them, in the same pattern language as old) leaves alone any occurrence that falls inside that text: replace(s, "<", "<", keep="<br>"). | result Str | Returns a Str. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
replace("the programme, the Programme, the PROGRAMME",
"programme", "program", case="preserve")
# "the program, the Program, the PROGRAM"
from collections-strings
replace("axb a.b", "a.b", "Z") # "axb Z" the dot is a dot
replace("axb a.b", "a.b", "Z", pattern="regex") # "Z Z"
replace("a1 b2 c3", "?#", "X", pattern="wildcard") # "X X X"
from collections-strings
env = jsonify(3 + 4j)
plain = replace(env, "\"type\":\"complex\",", "")
v = parse_json(plain)
print(plain) # {"re":3.0,"im":4.0}
print(type(v)) # record
{"re":3.0,"im":4.0}
recordfrom file-io
s = " Measurement Lane "
print("[{trim(s)}]") # [Measurement Lane]
print(upper(trim(s))) # MEASUREMENT LANE
print(contains(s, "Lane")) # true
print(replace(s, "Lane", "Window"))
[Measurement Lane] MEASUREMENT LANE true Measurement Window
from agent-guide
How it works
replace(s, old, new) — every occurrence of old replaced by new (literal substring match, not regex).
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.