REPLACE function
REPLACE swaps out a set number of characters at a given position, whatever those characters are. Use it to mask card numbers, change a fixed prefix, or insert a character into a code, such as a dash after the second character.
- Available in
- Every current version
- Category
- Text functions
- Returns
- Text with the replacement made.
- Google Sheets
- Works the same in Google Sheets.
Syntax
=REPLACE(old_text, start_num, num_chars, new_text)
| Argument | Required? | What it is |
|---|---|---|
old_text | Required | The text or cell to change. |
start_num | Required | Position of the first character to replace. The first character is 1. |
num_chars | Required | How many characters to replace. Use 0 to insert without removing anything. |
new_text | Required | The text to put in. |
Examples
Each result below is calculated from the sample sheet by our formula engine, the same one that grades the practice.
| Cell | Formula | Result | What it shows |
|---|---|---|---|
| D2 | =REPLACE(A2,1,12,"************") | ************4444 | Masks all but the last four digits |
| D3 | =REPLACE(B3,1,3,"651") | 651-555-0193 | A new area code in the first three characters |
| D4 | =REPLACE(C4,3,0,"-") | EF-9012 | num_chars of 0 inserts a dash without removing anything |
Try it live
This is the sample sheet. Change any number or text, or edit a formula, and the results update.
Show the sample data as a table
| Card number | Phone | Code | Result |
| 4111222233334444 | 612-555-0148 | AB1234 | ************4444 |
| 5500111122223333 | 763-555-0193 | CD5678 | 651-555-0193 |
| 3400999988887777 | 952-555-0107 | EF9012 | EF-9012 |
Practice REPLACE
Graded instantly, then re-checked on changed data.
Mask account numbers
B2, replace the first eight characters of the account number in A2 with XXXXXXXX, leaving the last four visible. Fill down to B6.Common mistakes
| What you see | Why, and how to fix it |
|---|---|
| Using it like find-and-replace | REPLACE doesn't search. =REPLACE(A2,"-",1,"") returns #VALUE! because "-" isn't a position. Use SUBSTITUTE to replace text by what it says. |
| Values of different lengths | A fixed start_num breaks when values vary in length. Calculate it instead: =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) masks all but the last four characters of any length. |
| Numbers turn into text | REPLACE returns text, so a changed number won't sum until you convert it with VALUE. |
Tips
- Set num_chars to 0 to insert text at a position without deleting anything.
Questions
How do I replace the first character in Excel?
=REPLACE(A2,1,1,"X") replaces the first character with X. To delete it, use =REPLACE(A2,1,1,"").
How do I mask a credit card number in Excel?
=REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) hides everything but the last four characters. Store card numbers as text, because Excel keeps only 15 digits of a number.
Last reviewed October 2026. Every example result on this page is calculated live by the Spreadsheet Practice formula engine.