Description
SUBSTITUTE function substitutes new_text for old_text in a text string. Use SUBSTITUTE when you want to replace specific text in a text string; use REPLACE when you want to replace any text that occurs in a specific location in a text string.
Syntax
SUBSTITUTE(text, old_text, new_text, [instance_num])
Parameters
Text Required. The text or the reference to a cell containing text for which you want to substitute characters.
Old_text Required. The text you want to replace.
New_text Required. The text you want to replace old_text with.
Instance_num Optional. Specifies which occurrence of old_text you want to replace with new_text. If you specify instance_num, only that instance of old_text is replaced. Otherwise, every occurrence of old_text in text is changed to new_text.
Examples
Example 1
The example may be easier to understand if you copy the example data (include header) in the following table, and paste it in cell A1 of a new Excel worksheet. If you need to, you can adjust the column widths to see all the data.
Data | Formula | Result | Description |
---|---|---|---|
Excel Bible | =SUBSTITUTE(A2,"Excel","Word") |
Word Bible | Substitutes Word for Excel. |
Quarter 1, 2014 | =SUBSTITUTE(A3, "1", "2", 1) |
Quarter 2, 2014 | Substitutes first instance of "1" with "2". |
Quarter 1, 2011 | =SUBSTITUTE(A4, "1", "2", 3) |
Quarter 1, 2012 | Substitutes third instance of "1" with "2". |
Excel | =SUBSTITUTE(A5, "e", "X") |
ExcXl | Substitutes X for e. |
Example 2: count specific characters in a cell
The example may be easier to understand if you copy the example data (include header) in the following table, and paste it in cell A1 of a new Excel worksheet. If you need to, you can adjust the column widths to see all the data.
Data | Formula | Result | Description |
---|---|---|---|
Google BOOKS | =LEN(A2)-LEN(SUBSTITUTE(A2,"o","")) |
2 | Count how many times "o" appears in a cell. |
Google BOOKS | =LEN(A3)-LEN(SUBSTITUTE(A3,"O","")) |
2 | Count how many times "O" appears in a cell. |
Google BOOKS | =LEN(A4)-LEN(SUBSTITUTE(UPPER(A4),"O","")) |
4 | Use UPPER to count lower and upper case. |
Google BOOKS | =LEN(A5)-LEN(SUBSTITUTE(LOWER(A5),"o","")) |
4 | Use LOWER to count lower and upper case. |
If you need to count how many times a specific character appears in a cell, you can do so with a formula that uses SUBSTITUTE and LEN.
SUBSTITUTE is a case sensitive function, so it will match case when running a substitution. If you need to count both upper and lower case occurrences of a specific character, use the UPPER (or LOWER) function inside SUBSTITUTE to convert the text to uppercase (or lowercase) before running the substitution. Then supply an uppercase (or lowercase) character as the text that's being substituted.