Guides Microsoft Flow Power Automate Power Platform

Remove Characters

How to remove a certain number of characters from the end of a string value in Microsoft Power Automate (Flow).

A quick guide on how to quickly cut characters from a string value in a Flow.

1. Initial Situation

Let’s say we have a dynamic content string value where we want to remove the last three characters.
An example for this would be a date where we don’t want the days, only year and months.

Example “Compose” action

So basically turn this:


in to:


To do this, we need to remove -06 from the first string.

2. Expression

The Expression we use, to get rid of a certain amount of characters at the end of a string is the following:


Let’s replace STRING in the above Expression with our dynamic content from the flow.

In our case, that’s the Compose – Date action.


If you want to remove, let’s say, 5 characters instead of only 3, just change the number 3 at the end of the Expression to 5. Like this:


The flow now looks like this:

Don’t forget to add the formula as an Expression!


When we now run the flow, we get the following result:

Check – we successfully cut the -06 from the string value

As you can see, the sign and the days have been removed from the string value.

Guides Microsoft Flow Power Automate Power Platform

Sanitize Strings

How to remove unwanted characters from a string in Microsoft Power Automate.

A quick guide on how to easily sanitize your text values in a Flow.

1. Initial Situation

Let’s say we have a string value. It does not matter from what trigger or action it comes from. In my case, I just have a Compose action with the following Input (value).

       amzAMZ160,.-;:_@)s9=)"*       =ç%&/"()=ç&Z"=                     

There are a lot of special characters and empty spaces in there that I do not want, but the end user has entered them anyways… 🙄 😀

2. Trim() – Remove all unnecessary empty spaces

The first thing I always do is use the trim() function on the string. This function removes all extra spaces from a string of text.

I do this right inside the Initialize variable action. Name it InitialString and as an Expression in the Value field, you enter the following code.


After the first two actions, our output now looks like this:

amzAMZ160,.-;:_@)s9=)"*       =ç%&/"()=ç&Z"=

Notice that the empty spaces at the front and at the back are not there anymore!

3. Map() – Create an array

Let’s now map our string and put every character as a single value in an array. This way, we can then check every character for its legitimacy later.

To create an array from a string value, we use the Select action with the following two expressions as From and Map values:


range(0, length(variables('InitialString')))


Make sure you click on Switch Map to text mode before you enter the following expression.

substring(variables('InitialString'), item(), 1)

Your Select action should now look like this:


Let’s check our output so far. After running the flow, we now have an array with every character of the initial string.

4. Allowed Characters

Let’s now define the characters that we will allow to remain in the string. For me, it’s all alphanumeric characters.

That means a-z, A-Z, and 0-9.

Create a new array (Initialize variable), call it AllowedCharacters, and put the following code as its Value as an Expression.


If you want to allow some special characters or more letters, you can simply edit the createArray() function to your liking. For example, to add ö,ä,ü,Ö,Ä and Ü the function would look like this:


After that step, I also add another new variable, this time a string variable, that will hold our sanitized value at the end. This will also be the variable that you will use after the sanitization for the rest of your flow.

Let’s call it ValueAfterSanitization. Value stays empty for now.

5. Remove any unwanted characters

This is where the magic happens…

We will loop through every character from the InitialValue array (created by the Select action) and check if it is in the AllowedCharacter array or not. If it is, we append it to the string variable ValueAfterSanitization.

To start the loop, add an Apply to each action. For its Value, choose the dynamic content Output from the Select action.

Add a Condition action inside the Apply to each.

  • The first value is the AllowedCharacters array
  • operator is contains
  • The second value is the expression: item()

This means we check each character (item()) if it is somewhere in the AllowedCharacters array.

In the If yes section of the Condition, we now add the character to the ValueAfterSanitization variable.

The If no section can stay empty, those item()s are the characters that we don’t want in our string.


Let’s do one last check. We add a Compose action with the Input ValueAfterSanitization.

At the top, we can see our trimmed string had 44 characters. We looped through each one of them and only added the characters we wanted to the ValueAfterSanitization variable.

In the last Compose action at the bottom, we can now see our sanitized string. Completely without special characters and empty spaces.

We went from this:

               amzAMZ160,.-;:_@)s9=)"*       =ç%&/"()=ç&Z"=                     

to this:


Conclusion / Summary

Instead of checking for characters that we don’t want to allow, we simply defined an array with the characters that we do allow.

This way, we don’t have to worry about different languages with weird (special) characters or emojis.

The whole Flow looks like this