SUBSTITUTE 與 REPLACE:Excel 和 Google Sheets 該用哪一個

SUBSTITUTE 按文本替換,並可指定第幾次;REPLACE 按位置和長度覆蓋。本次 SUBSTITUTE("banana","a","X") 得到 bXnXnX,指定第 2 次得到 banXna。REPLACE("banana",2,2,"XX") 得到 bXXana,從第 2 個字元起覆蓋 2 個字元。

本文目錄

快速解答

SUBSTITUTE 按文本替換,並可指定第幾次;REPLACE 按位置和長度覆蓋。本次 SUBSTITUTE("banana","a","X") 得到 bXnXnX,指定第 2 次得到 banXna。REPLACE("banana",2,2,"XX") 得到 bXXana,從第 2 個字元起覆蓋 2 個字元。

=SUBSTITUTE(B2,"a","X")      → bXnXnX
=SUBSTITUTE(B2,"a","X",2)    → banXna
=SUBSTITUTE(B2,"A","X")      → banana

檢查重點

SUBSTITUTE 區分大小寫:用 "A" 替換 banana 仍是 banana,用 "apple" 替換 Apple Pie 不變,用 "Apple" 得到 X Pie。REPLACE("SKU-103",4,1,"/") 得到 SKU/103;誤把連字元當成第 5 位時得到 SKU-/03。

邊界與相容性

SUBSTITUTE("SKU-103","-","") 得到 SKU103。REPLACE("Apple Pie",1,5,"Cake") 得到 Cake Pie。已知要改的文字時用 SUBSTITUTE,只知道位置時用 REPLACE。

完整英文範例、公式與作者備註

Which versions this applies to

The results below were measured in Google Sheets on November 12, 2026. Microsoft documents both SUBSTITUTE and REPLACE for Excel. I did not re-test these strings in desktop Excel today.

SUBSTITUTE changes by value, and can target one instance

B2 was banana:

=SUBSTITUTE(B2,"a","X")      → bXnXnX
=SUBSTITUTE(B2,"a","X",2)    → banXna
=SUBSTITUTE(B2,"A","X")      → banana

With no instance number, every a was replaced. With instance 2, only the second a became X (the first and third stayed). An uppercase "A" left the word unchanged, so the match is case-sensitive.

Removing a character is the same function with an empty replacement. SUBSTITUTE("SKU-103","-","") returned SKU103.

REPLACE changes by position

REPLACE does not look for a letter. It overwrites a slice:

=REPLACE(B2,2,2,"XX")     → bXXana
=REPLACE("SKU-103",4,1,"/")    → SKU/103
=REPLACE("Apple Pie",1,5,"Cake")  → Cake Pie

In banana, position 2 length 2 is the letters an, so they became XX. In SKU-103, the hyphen is the fourth character (S K U - …); REPLACE at position 4, length 1, put a slash there and returned SKU/103. I first tried position 5 by counting wrong and got SKU-/03 — the 1 was replaced, not the hyphen. Count the characters, or you will edit the wrong slice.

REPLACE("Apple Pie",1,5,"Cake") replaced Apple and left Pie, so the result was Cake Pie.

Letter case in SUBSTITUTE

B4 was Apple Pie:

=SUBSTITUTE(B4,"apple","X")    → Apple Pie
=SUBSTITUTE(B4,"Apple","X")    → X Pie

The lowercase search text did not match Apple. The exact-case search did. If you need a case-insensitive replace, I did not test a workaround in this file; I would not invent one. For case-sensitive lookups, see Case-Sensitive Lookup. For case-sensitive search inside a string, FIND vs SEARCH is the closer comparison (FIND is case-sensitive; SEARCH is not).

When to use which

  • You want every hyphen gone, or only the second comma changed: SUBSTITUTE.
  • You want whatever sits in positions 4 through 5 changed, even if the character varies from row to row: REPLACE.
  • You are tempted to use REPLACE to change a hyphen in SKU-103 without checking the position: do not. Position 5 was the digit 1 in this test.

Neither function splits a cell into columns. That is SPLIT / TEXTSPLIT.

Excel and Google Sheets differences

Microsoft documents the same two functions, including SUBSTITUTE’s optional instance number, for Excel. I am not quoting Excel strings from this run.

Common mistakes

  • Using REPLACE when you mean “change this letter wherever it is.” REPLACE starts at a position, not at a search string.
  • Off-by-one positions. REPLACE("SKU-103",5,1,"/") was SKU-/03, not SKU/103.
  • Expecting SUBSTITUTE to ignore case. "A" did not change banana; "apple" did not change Apple Pie.
  • Forgetting the instance argument and replacing every match. SUBSTITUTE("banana","a","X") changed all three as.

FAQ

What is the difference between SUBSTITUTE and REPLACE?

SUBSTITUTE finds text and replaces it, optionally only the nth instance. REPLACE overwrites a run of characters at a known position. SUBSTITUTE("banana","a","X",2) was banXna; REPLACE("banana",2,2,"XX") was bXXana.

How do I replace only the second occurrence?

=SUBSTITUTE(text,old,new,2). In banana, that turned only the second a into X.

Is SUBSTITUTE case-sensitive?

Yes, in this test. SUBSTITUTE("banana","A","X") returned banana. SUBSTITUTE("Apple Pie","apple","X") returned Apple Pie.

How do I change a hyphen in a SKU to a slash?

If every SKU has the hyphen in the same place, REPLACE(text,4,1,"/") returned SKU/103 for SKU-103. If the hyphen can move, SUBSTITUTE(text,"-","/") is safer; I did not run that exact slash substitution, but SUBSTITUTE("SKU-103","-","") did remove the hyphen.

Will these functions work in Excel?

Microsoft documents both for Excel. I did not re-run these strings in desktop Excel today.

Live-test findings

Google Sheets, November 12, 2026. SUBSTITUTE("banana","a","X") → bXnXnX; instance 2 → banXna; "A" → banana. REPLACE("banana",2,2,"XX") → bXXana. SUBSTITUTE("SKU-103","-","") → SKU103. REPLACE("SKU-103",5,1,"/") → SKU-/03; position 4 → SKU/103. SUBSTITUTE("Apple Pie","apple","X") unchanged; "Apple" → X Pie. REPLACE("Apple Pie",1,5,"Cake") → Cake Pie.

Sources

Oscar

專注電子表格公式與排錯;具體測試環境和待驗證事項見各篇文章。

Tested in Google Sheets, November 12, 2026. Excel version notes follow Microsoft documentation and were not re-tested in a desktop Excel workbook on this date..

本主題下一篇:資料清理與錄入 列出唯一值:Excel 與 Google Sheets