Description
REPLACE function replaces part of a text string, based on the number of characters you specify, with a different text string.
REPLACEB function replaces part of a text string, based on the number of bytes you specify, with a different text string.
Use REPLACE when you want to replace any text that occurs in a specific location in a text string.
Use SUBSTITUTE when you want to replace specific text in a text string.
What is the difference between REPLACE and REPLACEB?
REPLACE is intended for use with languages that use the single-byte character set (SBCS), whereas REPLACEB is intended for use with languages that use the double-byte character set (DBCS). The default language setting on your computer affects the return value in the following way:
- REPLACE always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
- REPLACEB counts each double-byte character as 2 when you have enabled the editing of a language that supports DBCS and then set it as the default language. Otherwise, REPLACEB counts each character as 1.
The languages that support DBCS include Chinese (Simplified), Chinese (Traditional), Korean, and Japanese.
Syntax
REPLACE(old_text, start_num, num_chars, new_text)
REPLACEB(old_text, start_num, num_bytes, new_text)
Parameters
Old_text Required. Text in which you want to replace some characters.
Start_num Required. The position of the character in old_text that you want to replace with new_text.
Num_chars Required. The number of characters in old_text that you want REPLACE to replace with new_text.
Num_bytes Required. The number of bytes in old_text that you want REPLACEB to replace with new_text.
New_text Required. The text that will replace characters in old_text.
Examples
Example 1: REPLACE
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 |
---|---|---|---|
1234567 | =REPLACE(A2,3,3,"*") |
12*67 | Replaces three characters, starting with the third character. |
abcdefghijk | =REPLACE(A3,1,4,"@") |
@efghijk | Replaces the first four characters with @. |
Excel | =REPLACE(A4,LEN(A4),1,"*") |
Exce* | Replaces the last characters with *. |
2013 | =REPLACE(A5,3,2,"14") |
2014 | Replaces the last two digits of 2013 with 14. |
Example 2: REPLACEB
=REPLACEB("中国香港",5,4,"北京")
REPLACEB returns "中国北京" because each character is counted as 2; the second argument specifies a starting point at the fifth byte, which is the third character, and the third argument specifies a length of 4 bytes, which is two characters.
=REPLACE("中国香港",5,4,"北京")
REPLACE returns "中国香港北京" because each character is counted as 1; the second argument specifies a starting point at the fifth character, and the third argument specifies a length of 4 characters. REPLACE returns "中国香港北京" no matter what the default language setting is on your computer.