Functions for Replacing in Strings
General strings functions and functions for searching in strings are described separately.
overlay
Replace part of the string input
with another string replace
, starting at the 1-based index offset
.
Syntax
Parameters
s
: A string type String.replace
: A string type String.offset
: An integer type Int (1-based). Ifoffset
is negative, it is counted from the end of the strings
.length
: Optional. An integer type Int.length
specifies the length of the snippet within the input strings
to be replaced. Iflength
is not specified, the number of bytes removed froms
equals the length ofreplace
; otherwiselength
bytes are removed.
Returned value
- A String data type value.
Example
Result:
Result:
overlayUTF8
Replace part of the string input
with another string replace
, starting at the 1-based index offset
.
Assumes that the string contains valid UTF-8 encoded text. If this assumption is violated, no exception is thrown and the result is undefined.
Syntax
Parameters
s
: A string type String.replace
: A string type String.offset
: An integer type Int (1-based). Ifoffset
is negative, it is counted from the end of the input strings
.length
: Optional. An integer type Int.length
specifies the length of the snippet within the input strings
to be replaced. Iflength
is not specified, the number of characters removed froms
equals the length ofreplace
; otherwiselength
characters are removed.
Returned value
- A String data type value.
Example
Result:
replaceOne
Replaces the first occurrence of the substring pattern
in haystack
by the replacement
string.
Syntax
replaceAll
Replaces all occurrences of the substring pattern
in haystack
by the replacement
string.
Syntax
Alias: replace
.
replaceRegexpOne
Replaces the first occurrence of the substring matching the regular expression pattern
(in re2 syntax) in haystack
by the replacement
string.
replacement
can contain substitutions \0-\9
.
Substitutions \1-\9
correspond to the 1st to 9th capturing group (submatch), substitution \0
corresponds to the entire match.
To use a verbatim \
character in the pattern
or replacement
strings, escape it using \
.
Also keep in mind that string literals require extra escaping.
Syntax
Example
Converting ISO dates to American format:
Result:
Copying a string ten times:
Result:
replaceRegexpAll
Like replaceRegexpOne
but replaces all occurrences of the pattern.
Alias: REGEXP_REPLACE
.
Example
Result:
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once, e.g.:
Result:
regexpQuoteMeta
Adds a backslash before these characters with special meaning in regular expressions: \0
, \\
, |
, (
, )
, ^
, $
, .
, [
, ]
, ?
, *
, +
, {
, :
, -
.
This implementation slightly differs from re2::RE2::QuoteMeta. It escapes zero byte as \0
instead of \x00
and it escapes only required characters.
For more information, see RE2
Syntax
format
Format the pattern
string with the values (strings, integers, etc.) listed in the arguments, similar to formatting in Python. The pattern string can contain replacement fields surrounded by curly braces {}
. Anything not contained in braces is considered literal text and copied verbatim into the output. Literal brace character can be escaped by two braces: {{ '{{' }}
and {{ '}}' }}
. Field names can be numbers (starting from zero) or empty (then they are implicitly given monotonically increasing numbers).
Syntax
Example
With implicit numbers:
translate
Replaces characters in the string s
using a one-to-one character mapping defined by from
and to
strings.
from
and to
must be constant ASCII strings.
If from
and to
have equal sizes, each occurrence of the 1st character of first
in s
is replaced by the 1st character of to
, the 2nd character of first
in s
is replaced by the 2nd character of to
, etc.
If from
contains more characters than to
, all occurrences of the characters at the end of from
that have no corresponding character in to
are deleted from s
.
Non-ASCII characters in s
are not modified by the function.
Syntax
Example
Result:
from
and to
arguments have different lengths:
Result:
translateUTF8
Like translate but assumes s
, from
and to
are UTF-8 encoded strings.
Syntax
Parameters
Returned value
- A String data type value.
Examples
Query:
printf
The printf
function formats the given string with the values (strings, integers, floating-points etc.) listed in the arguments, similar to printf function in C++. The format string can contain format specifiers starting with %
character. Anything not contained in %
and the following format specifier is considered literal text and copied verbatim into the output. Literal %
character can be escaped by %%
.
Syntax
Example
Query:
format
Introduced in: v20.1
Format the pattern
string with the values (strings, integers, etc.) listed in the arguments, similar to formatting in Python.
The pattern string can contain replacement fields surrounded by curly braces {}
.
Anything not contained in braces is considered literal text and copied verbatim into the output.
Literal brace character can be escaped by two braces: {{
and }}
.
Field names can be numbers (starting from zero) or empty (then they are implicitly given monotonically increasing numbers).
Syntax
Arguments
pattern
— The format string containing placeholders.String
s0[, s1, ...]
— One or more values to substitute into the pattern.Any
Returned value
Returns a formatted string. String
Examples
Numbered placeholders
Implicit numbering
printf
Introduced in: v24.8
The printf
function formats the given string with the values (strings, integers, floating-points etc.) listed in the arguments, similar to printf function in C++.
The format string can contain format specifiers starting with %
character.
Anything not contained in %
and the following format specifier is considered literal text and copied verbatim into the output.
Literal %
character can be escaped by %%
.
Syntax
Arguments
format
— The format string with%
specifiers.String
sub1, sub2, ...
— Optional. Zero or more values to substitute into the format string.Any
Returned value
Returns a formatted string. String
Examples
C++-style formatting
replaceAll
Introduced in: v1.1
Replaces all occurrences of the substring pattern
in haystack
by the replacement
string.
Syntax
Arguments
haystack
— The input string to search in.String
pattern
— The substring to find and replace.const String
replacement
— The string to replace the pattern with.const String
Returned value
Returns a string with all occurrences of pattern replaced. String
Examples
Replace all occurrences
replaceRegexpAll
Introduced in: v1.1
Like replaceRegexpOne
but replaces all occurrences of the pattern.
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once.
Syntax
Arguments
haystack
— The input string to search in.String
pattern
— The regular expression pattern to find.const String
replacement
— The string to replace the pattern with, may contain substitutions.const String
Returned value
Returns a string with all regex matches replaced. String
Examples
Replace all characters with doubled version
Empty substring replacement example
replaceRegexpOne
Introduced in: v1.1
Replaces the first occurrence of the substring matching the regular expression pattern
(in re2 syntax) in haystack
by the replacement
string.
replacement
can contain substitutions \0-\9
.
Substitutions \1-\9
correspond to the 1st to 9th capturing group (submatch), substitution \0
corresponds to the entire match.
To use a verbatim \
character in the pattern
or replacement
strings, escape it using \
.
Also keep in mind that string literals require extra escaping.
Syntax
Arguments
haystack
— The input string to search in.String
pattern
— The regular expression pattern to find.const String
replacement
— The string to replace the pattern with, may contain substitutions.const String
Returned value
Returns a string with the first regex match replaced. String
Examples
Converting ISO dates to American format
Copying a string ten times
translate
Introduced in: v22.7
Replaces characters in the string s
using a one-to-one character mapping defined by from
and to
strings.
from
and to
must be constant ASCII strings.
If from
and to
have equal sizes, each occurrence of the first character of first
in s
is replaced by the first character of to
, the second character of first
in s
is replaced by the second character of to
, etc.
If from
contains more characters than to
, all occurrences of the characters at the end of from
that have no corresponding character in to
are deleted from s
.
Non-ASCII characters in s
are not modified by the function.
Syntax
Arguments
s
— The input string to translate.String
from
— A constant ASCII string containing characters to replace.const String
to
— A constant ASCII string containing replacement characters.const String
Returned value
Returns a string with character translations applied. String
Examples
Character mapping
Different lengths
translateUTF8
Introduced in: v22.7
Like translate
but assumes s
, from
and to
are UTF-8 encoded strings.
Syntax
Arguments
s
— UTF-8 input string to translate.String
from
— A constant UTF-8 string containing characters to replace.const String
to
— A constant UTF-8 string containing replacement characters.const String
Returned value
Returns a String
data type value. String
Examples
UTF-8 character translation