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:
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.