Replace Text Or Characters in Power Automate

In Power Automate, you may sometimes need to clean or modify text before using it in your flow. Maybe you want to remove extra spaces, change a word, or replace special characters in a file name. In these cases, replace() help us.

In this tutorial, I will explain how to use the Replace() function in Power Automate. Also, I will cover an example below:

  • Replace a substring in a variable using Power Automate
  • Replace a character in a string in Power Automate
  • Replace with blank (remove a part of text) in Power Automate
  • Replace multiple characters in Power Automate
  • Replace only the nth occurrence of a character in Power Automate

Replace() Function in Power Automate

Power Automate replace() function returns a new string where every occurrence of a specified substring is replaced with another substring.

replace(text, oldText, newText)

Where:

  • text: The original string or variable to modify.
  • oldText: The substring you want to find and replace.
  • newText: The text that will replace every occurrence of oldText.

Use replace() whenever you need to clean or reformat text in a flow, for example, removing unwanted characters, updating names, or standardizing data before saving it to SharePoint, Excel, or sending it in an email.

For example, you have a variable varText that contains:

Hello World
Power Automate replace substring in a variable example

Now you want to change World to Power Automate, add a Compose action with this expression:

replace(variables('varText'), 'World', 'Power Automate')
How to replace a character in a string using Power Automate flow

Output:

Hello Power Automate
Power Automate expression to replace part of text with blank

This shows the basic use of replace() to swap one word for another in a string

Replace a Substring in a Variable using Power Automate

A SharePoint file name variable contains Project_Draft_Document, and you need it to show Project_Final_Document.

Now, I will only show you how to replace it if you want this example; please comment below.

  1. Create an Instant cloud flow -> choose Manually trigger a flow.
Remove specific text from a variable in Microsoft Power Automate
  1. Add an Initialize variable action and provide the following parameters:
    • Name: varFile
    • Type: String
    • Value: Project_Draft_Document.
Replace multiple characters at once in Power Automate string
  1. Add a Compose action with this expression:
replace(variables('varFile'), 'Draft', 'Final')
Power Automate formula to replace only the nth occurrence of a character

Click “Save” in the top-right corner of the Flow Designer. Click Test -> Manually -> Run flow.

Example of substring replacement in Power Automate workflow

Replace a Character in a String in Power Automate

Suppose you receive data Item|Price|Qty, but you need commas instead of the pipe (|).

  1. Go to Power Automate and click Create -> Instant Cloud Flow. Select Manually trigger a flow as the trigger.
  2. Next, add an Initialize Variable action and provide the parameters below:
    • Name: varData
    • Type: String
    • Value:
Item|Price|Qty
Microsoft Power Automate flow to replace keywords in a message body
  1. Then, add a compose action and provide the following expression:
replace(variables('varData'), '|', ',')
Power Automate replace text tutorial
  1. Click “Save” in the top-right corner of the Flow Designer. Click Test -> Manually -> Run flow.
  2. After the flow runs successfully, check the output of the compose action.
Power Automate compose action for replacing substring in variable

Replace with Blank (Remove a Part of Text) in Power Automate

For example, you have a file with names like SP_Report2025.pdf, and you want to remove the prefix ‘SP_’ from the file.

  1. Create an Instant cloud flow -> choose Manually trigger a flow.
  2. Add an Initialize variable action and provide the following parameters to store that file name:
    • Name: varName
    • Type: String
    • Value: SP_Report2025.pdf
Replace digits or numbers in a string with blank using Power Automate
  1. Add a compose action and provide the following expression:
replace(variables('varName'), 'SP_', '')
Remove extra spaces in a string using replace function Power Automate

OutPut:

Power Automate example to replace

Power Automate Replace Multiple Characters

For this example, I am cleaning a phone number (987) 654-3210 by removing parentheses and the dash.

Follow the steps below:

  1. Create an Instant cloud flow -> choose Manually trigger a flow.
  2. Add an Initialize variable action and provide the following parameters to store the file phone number:
    • Name: varPhone
    • Type: String
    • Value: (987) 654-3210
Replace multiple substrings in one step using Power Automate
  1. Add a compose action and provide the following expression:
replace(
    replace(
        replace(variables('varPhone'), '(', ''),
        ')', ''
    ),
    '-', ''
)

Where:

  • variables(‘varPhone’): This is your original phone number variable.
  • replace(variables(‘varPhone’), ‘(‘, ”): Removes all ( characters.
  • replace(, ‘)’, ”): Removes all ) characters.
  • replace(, ‘-‘, ”): Removes all – characters.
Power Automate expression to replace commas with semicolons

Output:

Replace special characters in Microsoft Power Automate string variable

This is the output, but it doesn’t look like this. I want like 9876543210.

If you also want to do this, add another Compose action and use the expression below:

trim(replace(outputs('Compose'),' ',''))
Power Automate flow to clean up text by replacing characters

Output after this expression:

Replace Multiple Characters in Power Automate

If you’d like, I can create a shorter version that removes all unwanted characters in one step, rather than nesting four replace() functions.

Replace Only the Nth Occurrence of a Character in Power Automate

Sometimes in Power Automate, you need to replace only a specific occurrence of a character or substring in a string.

  1. Go to Power Automate and click Create -> Instant Cloud Flow. Select Manually trigger a flow as the trigger. Click + Add an input and add the following inputs:
    • Text (Text) -> This will contain the string in which you want to replace the nth occurrence.
    • OldText (Text) -> The text or character you want to replace.
    • NewText (Text) -> The text that will replace OldText.
    • Number (Number) -> The occurrence number of OldText you want to replace.
Replace space with underscore in a string using Power Automate
  1. Add a Compose action named ReplaceNth and use this expression:
concat(
	slice(
		triggerBody()?['text'],
		0,
		nthIndexOf(triggerBody()?['text'],triggerBody()?['text_1'],triggerBody()?['number'])
	),
	triggerBody()?['text_2'],
	slice(
		triggerBody()?['text'],
		add(
			nthIndexOf(triggerBody()?['text'],triggerBody()?['text_1'],triggerBody()?['number']),
			length(triggerBody()?['text_1'])
		),
		add(
			length(triggerBody()?['text']),
			1
		)
	)
)

Where:

  • triggerBody()?[‘text’]: This is the original string from the trigger input.
  • triggerBody()?[‘text_1’]: This is the substring you want to replace.
  • triggerBody()?[‘number’]: This is the nth occurrence of the substring you want to replace.
  • nthIndexOf(triggerBody()?[‘text’], triggerBody()?[‘text_1’], triggerBody()?[‘number’]): Finds the position/index of the nth occurrence of the substring.
  • First slice(): Extracts the part of the string before the nth occurrence.
  • triggerBody()?[‘text_2’]: This is the replacement string for the nth occurrence.
  • Second slice(): This slices the part after the nth occurrence.
  • concat(…): Joins the three parts together.
Power Automate dynamic content replace text

After adding your trigger and Compose action (ReplaceNth), click Save in the top-right corner of Power Automate. Ensure there are no red error indicators. If everything is correct, the flow is now saved.

Click “Test” (next to “Save”). Choose Manually -> Click Test. Enter sample input values for the trigger inputs:

  • Text: Po1er Automate
  • OldText: 1
  • NewText: w
  • Number: 1
Power Automate to strip or remove unwanted characters

Click Run Flow. Once the flow runs successfully, click on the Compose (ReplaceNth) action to check the output.

Replace Only the Nth Occurrence of a Character in Power Automate

This method allows you to dynamically replace only the nth occurrence of a character or substring in any text.

The replace() function in Power Automate is a simple yet powerful way to clean and reformat text. Whether you need to swap a single word, remove special characters, or target only the nth occurrence of a substring, the steps are almost the same: create an instant flow, add the required inputs, and use the appropriate expression.

You may also like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App