3 Ways to Replace Characters in a String Using Power Automate

Do you need to replace characters in your text in a flow?

Often times you will need to clean your text data because your database doesn’t accept certain characters in the data.

This means you will need to remove unwanted characters to standardize text data. You might need to remove all commas, semicolons, quotation marks, or other characters from your text data.

Power Automate offers several functions to help you achieve this. The replace function is particularly useful, as it allows you to replace all occurrences of a specified character or string within a given text.

This post will show you how to replace all specified characters as well as a given instance of a character.

Replace All Instances of a Character with the Replace Function

If you want to replace all occurrences of a character in a string with Power Automate, you can use the replace function.

This function allows you to replace all instances of a character or characters from your text.

replace(string, oldText, newText)
  • string: The input string that needs to be modified.
  • oldText: The portion of the input string that needs to be replaced.
  • newText: The new string that will replace the oldText.
replace(outputs('Text'),', ','; ')

For example, the above expression will replace all the commas with a semi-colon from the text in the compose action.

The outputs('Text') is a reference to the text in the compose action. This contains the text to be modified.

The ', ' is the text to be replaced and is contained in single quote marks. This is replaced with '; '.

In this example, the text A, B, C, D, E will be transformed to A; B; C; D; E.

The replace function is an excellent way to replace all characters in a string with Power Automate. It is easy to use and can help you quickly modify your strings.

If you need to replace multiple characters, then you will need to use the replace function multiple times.

Replace Characters in a URL with the uriComponent Function

When working with URLs, it’s important to ensure that they are properly encoded to avoid issues with special characters. One way to achieve this is by using the uriComponent function in Power Automate.

This function replaces URL unsafe characters with escape characters, ensuring that the URL is properly encoded and safe to use.

To use the uriComponent function, simply pass the string containing the URL as an argument. The function will return the encoded URL as a string.

uriComponent('https://www.google.com/search?q=power tech tips')

For example, the above expression will encode the URL.

https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dpower%20tech%20tips

The example will return the above output as a URI-encoded string with escape characters. The special characters in the URL have been replaced with escape characters, making it safe to use.

By using this function you can easily replace URL-unsafe characters with escape characters.

The Replace Only the Nth Instance of a Character in a String

Replacing all characters or unsafe characters in a URL is an easy process as Power Automate provides functions for these cases.

Unfortunately, if you need to replace only the nth occurrence of a character in a string then the solution is much more complex.

The main idea of the solution is the following.

  • Find the location of the nth occurrence.
  • Split the string into two parts; the part before the nth occurrence and the part after the nth occurrence.
  • Now you can concatenate these two parts with the new string you want to replace.

You can use the nthIndexOf function to find the position of the nth occurrence of the character, then use the slice function to split the string at this point.

Then you can join these two parts with the concat function.

This example will show the items in separate compose actions, but in your situation, these might be hardcoded or in other dynamic content.

  1. This is a compose action named Text which contains the string in which you need to replace the nth occurrence of a text. This is referenced as outputs('Text').
  2. This is a compose action named OldText which contains the text you want to find and replace. This is referenced as outputs('OldText').
  3. This is a compose action named NewText which contains the new text that will replace the OldText. This is referenced as outputs('NewText').
  4. This is a compose action named Number which contains the number of the occurrence to replace. This is referenced as outputs('Number').

This example will replace the 3rd occurrence of , with ; in the string A, B, C, D, E and should result in the string A, B, C; D, E.

nthIndexOf(outputs('Text'),outputs('OldText'),outputs('Number'))
  1. The above expression will find the position of the nth occurrence. In the example, this would result in number 7.
slice(outputs('Text'),0,outputs('Index'))
  1. This expression will return the first part of the string before the nth occurrence. This example will return A, B, C.
slice(outputs('Text'),add(outputs('Index'),length(outputs('OldText'))),add(length(outputs('Text')),1))
  1. This expression will return the last part of the string after the nth occurrence. This example will return D, E.
concat(outputs('Left'),outputs('NewText'),outputs('Right'))
  1. This expression will return the string slices joined with the new text in the middle. This example will return A, B, C; D, E.

You don’t need to calculate these items separately. You can combine them into a single expression that returns the final result with the nth occurrence replaced.

concat(
	slice(
		outputs('Text'),
		0,
		nthIndexOf(outputs('Text'),outputs('OldText'),outputs('Number'))
	),
	outputs('NewText'),
	slice(
		outputs('Text'),
		add(
			nthIndexOf(outputs('Text'),outputs('OldText'),outputs('Number')),
			length(outputs('OldText'))
		),
		add(
			length(outputs('Text')),
			1
		)
	)
)

The above expression will replace the nth outputs('Number') occurrence of the old text outputs('OldText') with the new text outputs('NewText') within the text outputs('Text').

By using the nthIndexOf, slice, and concat functions in this way, you can easily replace only the nth occurrence of a character in a string with Power Automate.

Conclusions

You have learned how to replace characters in a string with the help of the replace function, you can easily modify any text field in your flow.

By using the replace function, you can replace all occurrences of a specific character in a string with new characters. This function can be used with any text string in your flow.

A more complex expression with the nthIndexOf, slice, and concat functions is required to replace a single occurrence in a string.

Have you needed to replace text in your flow? How did you do this? Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!