replace
replace, function
def pure const replace(source: text, pattern: text, replacement: text): text
Replaces, in the source text value, all the occurrences of the pattern with the replacement.
source: the text in whichpatternshould be replacedpattern: the pattern to replacereplacement: the text to insert as a replacement ofpattern
Example
table T = with
[| as Source , as Pattern , as Replacement |]
[| "Hello world!" , "l" , "L" |]
[| "Hello world!" , "L" , "W" |]
[| "Hello world!" , " " , "___" |]
[| "Hello world!" , " world" , "" |]
T.Result = replace(T.Source, T.Pattern, T.Replacement)
show table "T" with
T.Pattern
T.Replacement
T.Source
T.Result
This script displays the following table:
| Source | Pattern | Replacement | Result |
|---|---|---|---|
| Hello world! | l | L | HeLLo worLd! |
| Hello world! | L | W | Hello world! |
| Hello world! | ___ | Hello___world! | |
| Hello world! | world | Hello! |
Remarks
The search for pattern is case-sensitive, as illustrated in the second example above.
Calling replace with an empty replacement will simply remove any occurrence of the pattern in the source, as illustrated by the fourth example above.
Errors
Calling replace with an empty pattern is not allowed and will return the following error:
‘replace()’: empty pattern is not allowed.
Calling replace with arguments source, pattern and replacement such that replacing all occurences of the pattern in the source with the replacement would return a text longer than 256 characters will fail and return the following error:
‘replace()’: result exceeding 256 chars limit.
See also
- The SUBSTITUTE function of Excel, omitting the
instance_numargument.