How to add new line (‘\n’) to a string in Power Automate
Posted On April 28, 2021
“I am building a Power Automate flow and need to append values into a string variable, how can I add a new line/line feed into the variable”
When you build a string in Power Automate, sometimes you don’t want one long string. Having everything on a single line might be easier to work with, but terrible to read. And if user readability is important for your solution, you might need to add a few line breaks. In a reverse to replacing new lines, let’s take a look on adding new lines to a string.
Add new line in an action
The simplest solution is to add the new line directly in the editor. Enter all the dynamic content you want to turn into a string, including the format, into an action. You can use spaces, new lines, special characters… design the whole string visually.
Note: if you’re appending the string to a variable, don’t forget to add one extra new line at the end. The next string will start on the new line.
Add new line to concat(…) expression
Another approach to building a string is using an expression. Instead of a separate action to build it visually, you can use the concat(…) expression. It’ll take all the values separated by a comma and turn them into a string.
To add a new line into the string you must add the ‘new line’ character as one of the values. There’re three options how to do that.
Add ‘Enter’ into the expression
The first one is to add ‘Enter’ into the expression. Add opening single quote, press Enter on your keyboard, and then add the closing single quote. It’ll split the expression on multiple lines, but Power Automate will translate it as a new line character.
Create ‘new line’ variable and use it in the expression
The second approach is to create a new variable with the ‘new line’ character. Initialize a new string variable, and in the ‘Value’ field press Enter. Such variable can then be used in the concat(…) expression as a new line.
The third, and my preferred approach is to use the decodeUriComponent(‘%0A’) expression. As already explained in the article on replacing new lines in a string, the expression will give you the ‘new line’ character. With this expression you don’t need a special variable nor depend on the expression “layout”.
As you can see, it can be simple or not so simple to add a new line to a string in Power Automate. If you can format the string directly in an action, do it. It’s easy to design, easy to work with, and it can be done by everyone without the expressions knowledge. It’s an approach I use when building simple approval history on document libraries in plain text instead of html table.
The expression approach can get a bit confusing if you’re building strings from multiple pieces. As you can’t format the string visually, it’s quite easy to get lost in all the pieces of the expression. I’d recommend using it only if you can’t format the string in an action, e.g. when exporting data to .csv or creating an html table.
And as already mentioned in a note, if you’re appending to a string, don’t forget to add a new line at the end of each string.