Find and replace substrings within text.
s
(String
): The input string.replace
(String
): The replacement string.offset
(Int
): The 1-based starting position for the replacement. If negative, it’s counted from the end of s
.length
(Int
, optional): The number of characters to replace. If omitted, it defaults to the length of replace
.String
with the specified portion replaced.
Example:
modified_taco
replaces ‘taco’ with ‘soft’ starting at position 9.protein_swap
replaces ‘beef’ (4 characters) with ‘chicken’ starting at position 7.overlay
function is case-sensitive and works with byte positions, not character positions, for multi-byte encodings.s
(String
): The input UTF-8 encoded string.replace
(String
): The replacement string.offset
(Int
): The 1-based position where the replacement starts. If negative, it’s counted from the end of the string.length
(Int
, optional): The number of characters to replace. If omitted, it defaults to the length of the replace
string.String
with the specified portion replaced.
Example:
haystack
(String
): The original string to perform replacement on.pattern
(String
): The substring to search for.replacement
(String
): The string to replace the found substring with.pattern
replaced by replacement
.
Example:
replaceOne
replaces the first occurrence of ‘beef’ with ‘chicken’ in the original string.
replaceRegexpOne
with the (?i)
flag.haystack
(String
): The original string in which replacements will be made.pattern
(String
): The substring to be replaced.replacement
(String
): The string to replace the pattern with.pattern
replaced by replacement
. [String
]
Example:
replaceAll
replaces all occurrences of ‘Taco’ with ‘Burrito’ in the given string. Note that it only replaces the exact match ‘Taco’ and not ‘tacos’.
replaceRegexpAll
with an appropriate regular expression.haystack
(String
): The input string to perform the replacement on.pattern
(String
): The regular expression pattern to match (in re2 syntax).replacement
(String
): The string to replace the matched substring with.replacement
string can contain substitutions \0
-\9
:
\1
-\9
correspond to capturing groups (submatches) in the pattern.\0
corresponds to the entire match.\
character in the pattern or replacement, escape it with \\
.haystack
(String
): The input string to perform replacements on.pattern
(String
): The regular expression pattern to match (in re2 syntax).replacement
(String
): The string to replace matched substrings with.String
with all occurrences of the pattern replaced.
replacement
string can contain substitutions \0
-\9
:
\0
corresponds to the entire match\1
-\9
correspond to captured groups (submatches)\\
s
(String
): The input string to be escaped.String
]
Description:
This function adds a backslash before the following characters that have special meaning in regular expressions:
\0
, \
, |
, (
, )
, ^
, $
, .
, [
, ]
, ?
, *
, +
, {
, :
, -
re2::RE2::QuoteMeta
:\0
instead of \x00
regexpQuoteMeta
escapes the special regex characters +
, (
, )
, %
, and !
in the taco special description, making it safe to use in a regular expression pattern.
pattern
(String
): A string containing replacement fields surrounded by curly braces {}
.arg1, arg2, ...
(Any data type): Values to be inserted into the pattern.String
]
Details:
{0}
, {1}
, etc.{}
, {}
, etc.{{
and }}
.format
function creates a sentence describing a taco order by inserting the provided arguments into the pattern string.
from
and to
strings.
Syntax:
s
(String
): The input string to be transformed.from
(String
): A constant ASCII string defining the characters to be replaced.to
(String
): A constant ASCII string defining the replacement characters.String
]
from
and to
must be constant ASCII strings of the same length.from
and to
strings.
Syntax:
s
(String
): The input UTF-8 encoded string.from
(String
): A UTF-8 encoded string specifying characters to be replaced.to
(String
): A UTF-8 encoded string specifying replacement characters.String
with characters replaced according to the mapping.
Example:
from
and to
must be UTF-8 encoded strings of the same length.from
are not modified.from
, only the first occurrence is used.format
(String
): A format string containing format specifiers.arg1, arg2, ...
: Values to be formatted (strings, integers, floating-point numbers, etc.).%
and are followed by a type character (e.g., %s
for string, %d
for integer).%%
represents a literal %
character.%s
is replaced with string arguments (‘Juan’, ‘al pastor’, ‘pineapple’)%d
is replaced with the integer argument (3)