Spreadsheet Practice

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)

ArgumentRequired?What it is
old_textRequiredThe text or cell to change.
start_numRequiredPosition of the first character to replace. The first character is 1.
num_charsRequiredHow many characters to replace. Use 0 to insert without removing anything.
new_textRequiredThe 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.

CellFormulaResultWhat it shows
D2=REPLACE(A2,1,12,"************")************4444Masks all but the last four digits
D3=REPLACE(B3,1,3,"651")651-555-0193A new area code in the first three characters
D4=REPLACE(C4,3,0,"-")EF-9012num_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 numberPhoneCodeResult
4111222233334444612-555-0148AB1234************4444
5500111122223333763-555-0193CD5678651-555-0193
3400999988887777952-555-0107EF9012EF-9012

Practice REPLACE

Graded instantly, then re-checked on changed data.

Mask account numbers

In 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 seeWhy, and how to fix it
Using it like find-and-replaceREPLACE 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 lengthsA 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 textREPLACE 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.