How to Use the SUBSTITUTE Function in Google Sheets

Published on

If you don’t need pattern matching and want a simple text replacement in Google Sheets, SUBSTITUTE is the function to look for. Otherwise, you should check out REGEXREPLACE.

The purpose of the SUBSTITUTE function is to replace existing text with new text in a string. Let’s see how to use this function in Google Sheets.

Syntax and Arguments of the SUBSTITUTE Function

Syntax:

SUBSTITUTE(text_to_search, search_for, replace_with, [occurrence_number])

Arguments:

  • text_to_search : The text in which to search and replace.
  • search_for: The string to search for.
  • replace_with: The string that will replace the matching search string. Matching is case-sensitive.
  • occurrence_number: The occurrence of the search string to be replaced. This is optional and if omitted, all occurrences will be substituted.

Examples

Let’s use the following sentence as text_to_search:

“We are looking for light green trousers, a black T-shirt, and dark green socks.”

Assume it’s in cell A1.

The following formula will replace all occurrences of “green” (search_for) with “blue” (replace_with):

=SUBSTITUTE(A1, "green", "blue")

Output: “We are looking for light blue trousers, a black T-shirt, and dark blue socks.”

If you want to replace only the first occurrence of “green”, i.e., in “green trousers”, use the following formula:

=SUBSTITUTE(A1, "green", "blue", 1)

Output: “We are looking for light blue trousers, a black T-shirt, and dark green socks.”

Using the SUBSTITUTE function is that simple in Google Sheets.

SUBSTITUTE Function: Advanced Techniques and Best Practices

Here are some tips and tricks that will help you when using the SUBSTITUTE function extensively in Google Sheets:

  • When you want to search and remove a string, specify the replacement with an empty string ("").
  • Sometimes you might want to include a white space prefix before the search string. For example, if you want to replace “apple” but not “apple” in “pineapple”, you may consider adding a space before “apple” in the search string, like " apple".
  • You can repeat a string ‘n’ times using the SUBSTITUTE function in Google Sheets. Specify "" (empty string) in both search_for and replace_with and use a SEQUENCE formula in occurrence_number.

Example:

=ArrayFormula(SUBSTITUTE(A1, "", "", SEQUENCE(1, 5)))

This formula will repeat the text in cell A1 five times.

Streamlining OR Logic: Efficient Search Key Substitution

XLOOKUP is one of the popular options for lookup in Google Sheets.

Assume you have fruit names in column A and their corresponding expected arrival dates in column B. The data is sorted in ascending order based on the arrival dates.

To find the first arrival date of “Melon”, you can use the following formula:

=XLOOKUP("Melon", A:A, B:B)

And this one will return the first arrival date of “Blueberry”:

=XLOOKUP("Blueberry", A:A, B:B)

When you look through both results, you can find which one you receive first. If you want to do this in a single XLOOKUP, you can replace “Melon” in the range A:A with “Blueberry” and use that as the lookup range:

=ArrayFormula(XLOOKUP("Blueberry", SUBSTITUTE(A:A, "Melon", "Blueberry"), B:B))
Example of using the SUBSTITUTE function in an array formula and an XLOOKUP hack

Here we apply the SUBSTITUTE function in a column. So we must use the ARRAYFORMULA function as above.

Resources

Here are some relevant Google Sheets resources.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

4 COMMENTS

  1. Hi,

    How do replace a value and have the replacement appear in the same box that the value initially was? I notice that Substitute always shows up in its own box. For example, I have 0 in box A1 and want to replace 0 with 1 so that box A1 becomes 1. I want to do this on a very large scale and need to do it with formulas. Do you have any suggestions?

    Thanks.

  2. Hi There:

    I have a cell that has two texts that I need to eliminate to obtain a number; for instance, US$250.00$, I want to substitute “US$” and “$” all at once.

    I have been able to do it separately using the following formulas =SUBSTITUTE(H4,"US$","") and =SUBSTITUTE(I4,"$","").

    I would like to know if I can combine both formulas into one to make the work easier.

    Thanks

    • Hi, Cruz A Villasana,

      You can either nest the two Substitute formulas or use a Regexreplace formula instead.

      Using Substitute:

      =substitute(SUBSTITUTE(H4,"US$",""),"$","")*1

      Using Regexreplace:

      =REGEXREPLACE(H4, "[^\d\.-]+", "")*1

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.