How to remove trailing spaces and extra spaces in a string in Power Automate

Whenever we write any programs in any language, we frequently end up with string manipulation. And whenever there is string manipulation, you will bump into a requirement where you need to trim characters from the beginning and end of a string. Every language is equipped with powerful API’s to tackle these scenarios.

Power Automate is advanced workflow automation platform and it’s not an exception as well. In this blog I am going to focus on the below topics

  1. How to trim extra spaces from the start and end of a string in Power Automate?
  2. How to trim extra spaces between words of a string in Power Automate?

I am going to discuss on how to handle characters other than space in another blog. In this blog I am going to focus on space manipulations only on the string.

Whenever we think of Trim, we think of spaces. Removing extra space from beginning and end of a string. Power Automate have inbuilt functions to do the same and it’s quite powerful.

The first function is trim. Let’s see this function in action. Our first test will be with – ” The quick brown fox jumps over the lazy dog “. Quite evidently the string has multiple spaces at the start and end and also within the words.

As you can see from the below example, the Trim function as expected removes the spaces from the start and end but not within the string.

So the next question is quite obvious – How do I remove extra spaces from within the text. While there is no inbuilt API to do that it can be done using a simple workaround. Below are the steps I followed.

The first step is to split the whole text using space as the delimiter. Observe I am also using the trim function to remove trailing spaces.

Once we do that, we get the below output.

As you can see, we have the array created with empty item for each of the spaces. The next step is to filter out these empty items from the array. For that we use the “Filter Array” data operation. Check for the comment. I have specified the formula I have used in the comment.

The final step is to join the output from the above step to get the final string.

And below is the output of the final step.

Exactly as we thought.

You may also like the below posts.

Debajit Dutta
Business Solutions MVP