How to remove spaces from text in Power Automate
Use the trim() expression to remove spaces from the start and end of a text value. Write trim(' Hello ') to get 'Hello'. To remove every space, including the ones in the middle, use replace() instead.
What does the trim() function do?
The trim() function removes leading and trailing whitespace from a string and returns the cleaned text. Microsoft documents it as trim('<text>').
Spaces between words stay in place. trim(' Hello World ') returns 'Hello World', not 'HelloWorld'.
How do you trim a SharePoint column value in a flow?
Use the Compose output in later steps, such as a filter or an update. Test with a value that has spaces at both ends so you can see the change.
- Place a Compose action below your trigger or Get item action.
- Click inside its input, then switch to the Expression tab.
- Enter trim(triggerOutputs()?['body/Title']), using your own column name instead of Title.
- Choose Add, then save the flow and run it.
- Look at the Compose result in run history.
How do you remove all spaces from text?
Use replace(). Write replace(variables('Phone'), ' ', '') to swap every space for nothing. The second value is one space between quotes. The third value is two quotes with nothing between them.
Microsoft documents replace() as case-sensitive. That matters for letters, not for spaces.
To remove dashes and spaces from a phone number, nest the calls: replace(replace(variables('Phone'), ' ', ''), '-', '').
Why does trim() leave a space behind?
Some spaces are not the normal space character. Text copied from web pages, Excel or email can hold a non-breaking space, which is character code 160. If a space survives trim(), this is a common cause.
Replace it first. Write replace(variables('Text'), decodeUriComponent('%C2%A0'), ' '), then wrap the result in trim(). Test with your own data, because the exact character depends on where the text came from.
Why trim text before you compare it?
Two values that look the same can differ by one trailing space. A condition that compares them reports a mismatch. A lookup in Get items can return no rows for the same reason.
Trim both sides of the comparison: equals(trim(variables('A')), trim(variables('B'))). To test for a blank value after trimming, see how to test for an empty field.
A null input makes trim() fail. Read the invalid type error about null and guard the input with coalesce(): trim(coalesce(variables('Text'), '')).
What can you do after the text is clean?
Cut part of the text with substring(), or break it at a separator with split(). Trim first so stray spaces do not end up in the pieces.
The split() function leaves spaces around each piece when the separator has spaces nearby. Trim each piece before you use it.
FAQ
Does trim() remove spaces in the middle of text?
No. It removes only leading and trailing whitespace. Use replace() to remove spaces inside the text.
How do I remove all spaces in Power Automate?
Write replace(text, ' ', ''). It swaps every space for nothing.
Why does my text still have a space after trim()?
It may be a non-breaking space (code 160) from a web page or spreadsheet. Replace it with a normal space, then trim.
What happens if I trim a null value?
The action fails. Wrap the input in coalesce() with an empty string as the fallback.