How to Use the REPLACE Function in Google Sheets | Replace Text by Position
How to Use the REPLACE Function
The REPLACE function replaces a specified number of characters starting at a given position in a text string with new text. It is useful for masking parts of phone numbers or credit card numbers, as well as updating specific digits in fixed-length codes.
Syntax
=REPLACE(text, position, length, new_text)
- text: The original text string or cell reference (required).
- position: The starting position from the beginning of the text, as an integer of
1or greater (required). - length: The number of characters to replace (remove) starting from
position, as an integer of0or greater (required). - new_text: The new string to insert (required). Passing an empty string
""deletes the specified segment.
Example Using a Table
Here is an example of masking the middle 4 digits (4 characters starting at position 5) of phone numbers with ****.
| Row | Column A (Phone Number) | Column B (Masked) |
|---|---|---|
| 1 | Phone Number | Masked |
| 2 | 090-1234-5678 | =REPLACE(A2, 5, 4, "****") → 090-****-5678 |
| 3 | 080-9876-5432 | =REPLACE(A3, 5, 4, "****") → 080-****-5432 |
| 4 | 070-1111-2222 | =REPLACE(A4, 5, 4, "****") → 070-****-2222 |
- Formula:
=REPLACE(A2, 5, 4, "****") - Result:
090-****-5678
Also, setting length to 0 inserts text without deleting any existing characters—for example, =REPLACE("20261122", 5, 0, "-") inserts a hyphen to produce 2026-1122.
Common Errors and Notes
position< 1or NegativelengthCauses#VALUE!: Passing0or less forposition, or a negative number forlength, causes a#VALUE!error.- When
positionExceeds the String Length: No error occurs;new_textis simply appended to the end of the original string. - No Distinction Between Full-Width and Half-Width Characters: Both
positionandlengthcount every character equally as 1 character.
Frequently Asked Questions
Q. Can REPLACE be used when the replacement text has a different length than the removed text?
A. Yes, without any issue. For example, you can remove 3 characters and replace them with 6 new characters, or set the length to 0 to insert text at a specific position.
Q. When should I use REPLACE versus SUBSTITUTE?
A. Use REPLACE when the target is defined by position (such as characters 4 through 7 from the start), and use SUBSTITUTE when you want to match specific text content (such as removing all hyphens).