Remove All spaces from string in PowerAutomate

When working with Power Automate, it’s common to encounter scenarios where you need to manipulate text data—such as removing unwanted spaces—before further processing it. Whether you’re concatenating fields or dealing with user input the unnecessary internal spaces can cause issues like inconsistent formatting or errors in systems. so in this blog post, we will learn how to remove all spaces in string and format the text data.

Remove All spaces from string in PowerAutomate

In my scenario, I am using Power Automate to concatenate three fields—first name, second name (first character only), and last name—into a single string. However, after performing the concatenation, I noticed that extra spaces are still present at the beginning or throughout the result.

Remove All spaces from string in PowerAutomate

If you want to remove all spaces from the concatenated result (including spaces between words), you can use the replace() function in Power Automate to replace ‘ ‘, ”.

In this expression:

  • concat() is used to combine the three fields (cr52c_firstname, cr52c_secondname, cr52c_lastname).
  • replace() removes all spaces (' ') from the concatenated result.
replace(concat(items('Apply_to_each')?['cr52c_firstname'], substring(items('Apply_to_each')?['cr52c_secondname'],0,1), items('Apply_to_each')?['cr52c_lastname']), ' ', '')
Remove All spaces from string in PowerAutomate

But I noticed while this works for removing all spaces, it doesn’t address the leading and trailing spaces in individual fields before concatenation.

A more efficient approach would be to trim the individual fields before concatenating them. This ensures that any leading or trailing spaces are removed before the string is joined together.

Power Automate provides a built-in function, trim(), which removes leading and trailing spaces from text.

replace(concat(trim(items('Apply_to_each')?['cr52c_firstname']), trim(substring(items('Apply_to_each')?['cr52c_secondname'],0,1)), trim(items('Apply_to_each')?['cr52c_lastname'])), ' ', '')
spaceinstring 1 | Power Platform Geeks

Conclusion

In Power Automate, spaces can be a tricky issue, especially when dealing with concatenation. By using the trim() function effectively on each field or the concatenated result, you can clean up unwanted spaces and ensure your output is formatted correctly. The replace() function is useful if you need to remove all spaces, but be cautious, as it will remove internal spaces as well, which might not always be the desired behavior.

See Also
Join us

Heba Kamal

Microsoft MVP, MCT, Technical Speaker, Blogger, and Microsoft 365 and Power Platform Consultant.

Leave a Reply