Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

How to replace new line (‘\n’) in a Power Automate expression

Posted on April 11, 2021July 30, 2021 by Tom

“I can’t figure out how to replace a new line (‘\n’) or any other non-printable character in Power Automate expression.”


The Power Automate expression replace(…) doesn’t work as you might expect for some non-printable characters, e.g. new line. The first thing you’d probably try is to replace ‘\n’. But Power Automate won’t process it as a new line, it’ll process it as it is: \n string. Replace(‘string’, ‘\n’, ”) will replace only the substring \n in the whole string. If the substring is not found, it’ll not replace anything and the flow will continue. To replace a new line you must use the right ‘new line’ character in your flow.

Store ‘new line’ in a variable

One of the solutions is to store the ‘new line’ into a variable and then use that variable in the replace(…) expression. Initialize a String variable, and in the ‘Value’ just press Enter. That’ll create a variable with a character representing a ‘new line’.

New line variable

You can then use this variable in the replace(…) expression to replace new lines, e.g. with a semicolon.

replace(outputs('Compose'),variables('var_newLine'),';')
Power Automate replace new line variable

Get the ‘new line’ from an expression

Another approach, if you don’t want to create an extra variable in your flow, is to get the ‘new line’ from an expression. You can get the ‘new line’ character by a conversion from its percent-encoded (Uri) value ‘%0A’ via decodeUriComponent(‘Uri value’) expression.

decodeUriComponent('%0A')

Instead of initializing a variable, you use decodeUriComponent(…) directly in the replace(…) expression. The example below will replace new lines with a semicolon.

replace(outputs('Compose'),decodeUriComponent('%0A'),';')
Power Automate replace new line expression

Note: some files created in Windows use \r\n instead of just \n as the ‘new line’ value. If you end up with \r in your string in place of the new lines, try to replace the whole \r\n substring:

decodeUriComponent('%0D%0A')

Summary

Since this blog’s motto is “From no-code to low-code”, my preference is the expression. It’s not only reducing number of variables, but I’d consider it even safer to manage (“Why has the variable no value? I think I’ll delete it…”).

Also, the expression is not limited only to the ‘new line’ character. You can use the decodeUriComponent(…) for any problematic character, e.g. apostrophe. Instead of many apostrophes (as used e.g. in the Filter Query with apostrophes), you can decode the ‘%27’ value anywhere in your flow. You just need to find the percent-encoded value to replace.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

5 thoughts on “How to replace new line (‘\n’) in a Power Automate expression”

  1. Ben says:
    April 13, 2021 at 3:09 pm

    Good tip! I’ve run into this before and ended up using the variable method, but the expression makes a lot of sense.

    Reply
  2. Roberto says:
    April 16, 2021 at 2:48 pm

    Great find. Wanted to share that if the output is from the Create CSV Table action then the following worked for me:
    replace(body(‘Create_CSV_table’),decodeUriComponent(‘%0D%0A’),’;’)

    The \r\n is successfully replaced with ‘;’

    Reply
  3. JK says:
    April 26, 2021 at 8:43 am

    Utterly brilliant post that has finally helped me solve a problem that I’ve been trying to solve for hours. Thanks for helping everyone

    Reply
  4. Mike says:
    December 15, 2021 at 8:19 pm

    Thanks! This saved a few hours of head scratching today.

    Reply
  5. Shengqi Tao says:
    January 17, 2022 at 8:42 am

    Thanks! It works fine!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Check if email already exists in Outlook contacts (Power Automate)August 7, 2022
  • Set permissions for a SharePoint list or library with Power AutomateAugust 3, 2022
  • Handle deleted files in synchronised SP library in Power AutomateJuly 31, 2022
  • Create item with lookup column in a dynamic list (Power Automate)July 27, 2022
  • How to move list between SharePoint tenants with Power AutomateJuly 24, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme