2 Ways to Remove Spaces from a String in Power Automate

Do you need to remove space characters from a string in Power Automate?

Removing spaces from strings is a common task in text manipulation.

Whether it’s leading and trailing whitespace you’re looking to eliminate or if you want to remove spaces from within a string, Power Automate has the tools to achieve this.

To remove spaces from a string you can use the built-in trim and replace functions.

The trim function is primarily used to remove leading and trailing whitespace, while the replace function can be utilized to remove all spaces from within the string.

This post will show you how you can use these functions to remove spaces from strings effectively.

How to Use the Replace Function to Remove Spaces

The replace function allows you to find and replace any substring within a string.

You can remove spaces from a string using the replace function. It has a simple syntax.

Replace Function Syntax

replace(<text>, <oldText>, <newText>)

The replace function has three mandatory input parameters: the original string <text>, the old string <oldText>, and the new string <newText>.

  • <text>: The input string that needs to be modified.
  • <oldText>: The text that needs to be replaced within the <text>.
  • <newText>: The text that replaces the <oldText> that is found in the <text>.

The function replaces all occurrences of the <oldText> in the text with the <newText>.

To remove spaces from a string, you need to define the <oldText> string as a space ' ' and the <newText> as an empty string ''. This will remove all spaces from the <text>.

Replace Function Example

replace(' Hello World ! ', ' ', '')

The above example will return the text without any spaces as HelloWorld!. This means it will remove all leading, trailing, and middle spaces from the given text.

replace(outputs('text'), ' ', '')

The above example will remove all the spaces from a string in a Compose action named text.

Keep in mind that the replace function replaces all instances of the specified character. In this case, all spaces in the string will be removed.

💡 Tip: Check out this post if you only want to remove a specific instance of a space character.

How to Use the Trim Function to Remove Leading and Trailing Spaces

You can use the trim function to remove only the leading and trailing space characters from a string while keeping the spaces within the text.

This is a useful option when you don’t want to remove all the spaces within your string.

Trim Function Syntax

The trim function allows you to remove all leading and trailing whitespace characters from a string.

trim(<text>)
  • <text>: The string that should be trimmed (removal of leading and trailing whitespace).

The trim function takes a single input parameter, which is the string that needs to be trimmed. The function removes any leading and trailing whitespace from the string and returns the modified string.

Trim Function Example

trim('   Hello!  ')

The above example will remove the 3 spaces in front and the 2 spaces at the end then return the text Hello!.

trim(outputs('text'))

The above example will remove all the spaces from the text in the Compose action named text.

Conclusions

Removing spaces from a string is a simple yet essential task. You can accomplish this by using either the trim or replace depending on the situation.

The trim function can be used when you need to remove only leading or trailing spaces from your text. The replace function can be used when you need to remove all space characters.

Have you needed to eliminate space characters from your flow data? How did you do it? 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!