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

Now you want to change World to Power Automate, add a Compose action with this expression:
replace(variables('varText'), 'World', 'Power Automate')

Output:
Hello Power Automate

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.
- Create an Instant cloud flow -> choose Manually trigger a flow.

- Add an Initialize variable action and provide the following parameters:
- Name: varFile
- Type: String
- Value: Project_Draft_Document.

- Add a Compose action with this expression:
replace(variables('varFile'), 'Draft', 'Final')

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

Replace a Character in a String in Power Automate
Suppose you receive data Item|Price|Qty, but you need commas instead of the pipe (|).
- Go to Power Automate and click Create -> Instant Cloud Flow. Select Manually trigger a flow as the trigger.
- Next, add an Initialize Variable action and provide the parameters below:
- Name: varData
- Type: String
- Value:
Item|Price|Qty

- Then, add a compose action and provide the following expression:
replace(variables('varData'), '|', ',')

- Click “Save” in the top-right corner of the Flow Designer. Click Test -> Manually -> Run flow.
- After the flow runs successfully, check the output of the compose action.

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.
- Create an Instant cloud flow -> choose Manually trigger a flow.
- Add an Initialize variable action and provide the following parameters to store that file name:
- Name: varName
- Type: String
- Value: SP_Report2025.pdf

- Add a compose action and provide the following expression:
replace(variables('varName'), 'SP_', '')

OutPut:

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:
- Create an Instant cloud flow -> choose Manually trigger a flow.
- Add an Initialize variable action and provide the following parameters to store the file phone number:
- Name: varPhone
- Type: String
- Value: (987) 654-3210

- 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.

Output:

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'),' ',''))

Output after this expression:

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.
- 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.

- 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.

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

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

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:
- Create SharePoint Term in Term Store Using Power Automate
- Create Indexed Columns in SharePoint Using Power Automate
- Add a New Attendee to a Meeting Without Email Others Using Power Automate
- Retrieve SharePoint List Items by Created Date Range Using REST API
- Set No Selected Item in a Power Apps Gallery

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.