
SUBSTITUTE
The SUBSTITUTE function returns a string value where the specified characters of a given string value have been replaced with a new string value.
SUBSTITUTE(source-string, existing-string, new-string, occurrence)
source-string: Any value.
existing-string: The value to be replaced. existing-string can contain any value.
new-string: The value used as a replacement. new-string can contain any value. It does not have to be the same length as existing string.
occurrence: An optional number value specifying which occurrence of existing-string within source-string should be replaced. occurrence must be greater than or equal to 1, or omitted. If occurrence is greater than the number of times existing-string appears within source-string, no replacement occurs. If occurrence is omitted, all occurrences of existing-string within source-string are replaced by new-string.
Notes
You can replace individual characters, whole words or strings of characters within words.
If REGEX is used to specify the search value in existing-string, capture groups can be used in new-string.
Examples |
---|
=SUBSTITUTE("a b c d e f", "b", "B") returns "a B c d e f". =SUBSTITUTE("a a b b b c", "a", "A", 2) returns "a A b b b c". =SUBSTITUTE("a a b b b c", "b", "B") returns "a a B B B c". =SUBSTITUTE("aaabbccc", "bc", "BC", 2) returns "aaabbccc". =SUBSTITUTE(60606, 6, 3) returns "30303". =SUBSTITUTE("example@example.com: Marina Email", REGEX("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}") & REGEX(": *"), "") returns "Marina Email". =SUBSTITUTE("marina@example.com", REGEX("([A-Z0-9a-z._%+-]+)@([A-Za-z0-9.-]+\.[A-Za-z]{2,4})"), "$1") returns "marina". By using the first capture group in the REGEX, you extract only the username. =SUBSTITUTE("marina@example.com", REGEX("([A-Z0-9a-z._%+-]+)@([A-Za-z0-9.-]+\.[A-Za-z]{2,4})"), "$2") returns "example.com". By using the second capture group in the REGEX, you extract only the domain name. |