How to trim spaces from the end and within a text in PowerApps Canvas app

Do you know the following?

  • Trim() function in canvas apps not only removes text from the end but also from within the text?
  • There is a separate function TrimEnds() and how does it differ from Trim()?
  • How do you trim spaces from the items inside a collection?

If you are aware of all the three points, you may skip reading the post. I don’t mind though you still continuing 🙂

First I will start with the Trim() function. If you are someone coming from a programming background, it’s almost guaranteed that you must have worked with API’s to trim spaces from text. Contrary to the usual behavior in majority of the scenarios, the Trim() function does what it is expected of it and even more.

I use the below string for Testing – ” The quick brown fox jumps over the lazy dog “. The text contains extra spaces at the beginning and end as well as extra spaces within the text.

I use a label and in the Text property I use the Trim() function on the text specified above.

Trim() function in canvas apps

Observe the text displayed in the label. Trim() function removes not only the spaces at the start and end, even the extra spaces from within the text. Wonderful isn’t it?

But wait. You may be thinking, is there no way that spaces within the text can be spared? Don’t be disappointed. There is a specific function in Canvas apps which do that. And that function is TrimEnds().

Trim spaces in canvas apps.

A relief right. But the show is not over. What if you need to trim items of a collection?

For this demo, I create a collection as below

Trim spaces in canvas apps.

And then set the Text of the label to the below formula

Quite obvious, the text contains the spaces within each item. What if I want to Trim the items? The answer is you can use the Trim function and pass the collection inside the trim.

Wonderful isn’t it? All the individual items inside the collection are trimmed with spaces from the start and end and also extra spaces within the text.

Same is the case with TrimEnds()

You may also like the below posts.

Hope this helped!

Debajit Dutta
Business Solutions MVP