What is the REPLACE function?
=REPLACE(old_text, start_num, num_chars, new_text)REPLACE removes a number of characters from a given position in a text and puts new text in their place.
Where SUBSTITUTE finds and replaces specific characters, REPLACE works by position: "from the nth character, this many characters". Drag across the strip above to choose the part to replace.
Arguments
- old_text
- The original text to change.
- start_num
- The position to start replacing. The first character is 1.
- num_chars
- How many characters to remove. 0 only inserts.
- new_text
- The text to put in place of the removed characters.
REPLACE examples
Example 1. Mask a phone number
=REPLACE("555-123-4567", 5, 3, "***")"555-***-4567".
Example 2. Example from Microsoft
=REPLACE("abcdefghijk", 6, 5, "*")Replaces 5 characters from the 6th with *: "abcde*k".
Example 3. Insert characters
=REPLACE("20261118", 5, 0, "-")num_chars is 0, so the result is "2026-1118".
Common mistakes
Thinking num_chars is the new text length
num_chars is how many characters to remove. new_text goes in whole, whatever its length.
Confusing it with SUBSTITUTE
Use SUBSTITUTE when you know the characters but their position varies, and REPLACE when the position is fixed.
FAQ
- What if start_num is past the end?
- new_text is added at the end.
- Is there a byte-based version?
- Yes, REPLACEB counts bytes in some languages.