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.

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

Real-Time Excel Filtering Using Combo Box & FILTER Function

In this tutorial, you’ll learn how to set up real-time Excel filtering using a...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

More like this

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

How to Extract Top N from Aggregated Query Results in Google Sheets

To summarize data in Google Sheets, you can use the QUERY function. However, if...

How to Use RANK IF in Google Sheets (Conditional Ranking)

You can use the RANK function to rank values in an entire dataset. But...

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.