Power Automate: Remove Characters From A String
A Power Automate flow can unexpectedly fail to due invalid characters in a file name. To solve this problem we can obtain the file name and use Power Automate to remove characters from a string. I will show you two approaches to do this: defining a list of non valid characters and removing them from a filename and creating a list of valid characters and keeping them in the file name.
Table of Contents
• Introduction: The File Name With Special Characters
#1 Remove Invalid Characters Method
• Remove Characters From A String In Power Automate
• Flow Expressions To Remove Characters From A String
• Run The Flow To Remove Characters From A String
• Understanding How The Flow To Remove Characters Works
#2 Keep Only Valid Characters Method
• Keep Only Valid Characters In A Text String
• Flow Expressions To Keep Only Valid Characters In A Text String
• Run The Flow To Keep Only Valid Characters In A String
• Explaining The Flow To Keep Only Valid Characters In A String
Introduction: The File Name With Special Characters
A member of the Technology department uses a Power Automate flow to ensure filenames have only valid characters.
The flow removes any special characters from the filename.
#1 Remove Invalid Characters Method
Remove Characters From A String In Power Automate
Open Power Automate and create a new instant flow to remove the non valid characters from a text string. Add all of the actions shown the image below to the flow. It will take the following steps:
- Compose a string of the non valid characters to be removed from the text string
- Pass in the string value to be cleaned
- Check each character in the string value one-by-one
- Filter and remove any non-valid characters
Flow Expressions To Remove Characters From A String
Copy and paste these text values and flow expressions into the appropriate actions to quickly build the flow.
Compose: Non Valid Characters – Inputs field
~`!@#$%^&*()+|{}[]<>/\?;:.,
Compose: Text String To Validate – Inputs field
data$batch!-2024.03.02
Select: Array Of Text String Chars To Validate – From field
range(0,length(outputs('Compose:_Text_String_To_Validate')))
Select: Array Of Text String Chars To Validate – Map field – Index value
indexOf(outputs('Compose:_Non_Valid_Characters'), substring(outputs('Compose:_Text_String_To_Validate'),item(),1))
Filter array – Index Equals -1 (choose a value field)
item()['Index']
Select: Array Of Text String Chars To Validate – Map field – Char value
substring(outputs('Compose:_Text_String_To_Validate'),item(),1)
Compose: Valid Text String – Inputs field
join(body('Select:_Array_Of_Valid_Characters'), '')
Run The Flow To Remove Characters From A String
After the flow is created give it a test to run ensure it works as expected. The text string in our example data$batch!-2024.03.02 is changed to databatch-20240302.
Understanding How The Flow To Remove Characters Works
The flow successfully removes non valid characters from a string but how does it work? The flow action Select: Array Of Text String Chars To Validate builds an array of characters in the text string paired with an index number. An index of -1 means the character was not found in the string of non valid characters. Indexes with a value not equals to -1 have a non valid character.
Then we filter the array to show only valid characters in the action Filter array: Index Equals -1.
The filtered array is transformed into a simple array of characters in Select: Array Of Valid Characters which are re-joined into a text string in the action Compose: Valid Text String.
#2 Keep Only Valid Characters Method
Keep Only Valid Characters In A Text String
Compiling a list all of the non valid characters which can be found in a string is difficult. It is simpler to approach the problem by defining a list of valid characters instead. We will create another flow to illustrate this method.
Open Power Automate and create a new instant flow to remove the non valid characters from a text string. Add all of the actions shown the image below to the flow
Flow Expressions To Keep Only Valid Characters In A Text String
Copy and paste these text values and flow expressions into the appropriate actions to quickly build the flow.
Compose: Valid Characters – Inputs field
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-
Compose: Text String To Validate – Inputs field
data$batch!-2024.03.02
Select: Array Of Text String Chars To Validate – From field
range(0,length(outputs('Compose:_Text_String_To_Validate')))
Select: Array Of Text String Chars To Validate – Map field – Index value
indexOf(outputs('Compose:_Valid_Characters'), substring(outputs('Compose:_Text_String_To_Validate'),item(),1))
Filter array: Index Does Not Equal -1 (choose a value field)
item()['Index']
Select: Array Of Text String Chars To Validate – Map field – Char value
substring(outputs('Compose:_Text_String_To_Validate'),item(),1)
Compose: Valid Text String – Inputs field
join(body('Select:_Array_Of_Valid_Characters'), '')
Run The Flow To Keep Only Valid Characters In A String
Now that the flow is complete perform a test to run ensure it works properly. Once again, the text string in our example data$batch!-2024.03.02 is changed to databatch-20240302.
Explaining The Flow To Keep Only Valid Characters In A String
Just as before, the flow action Select: Array Of Text String Chars To Validate builds an array of characters in the text string paired with an index number. An index of -1 means the character was not found in the string of non valid characters. Indexes with a value not equals to -1 have a non valid character.
But this time we filter the array differently to show only characters that do not equal -1 in the action Filter array: Index Does Not Equal -1.
Finally we, reconstruct the simple array of characters in Select: Array Of Valid Characters are once again joined into a text string in the action Compose: Valid Text String.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps & Power Automate articles sent to your inbox each week for FREE
Questions?
If you have any questions or feedback about Power Automate: Remove Characters From A String please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.
Could You provide expressions inside Filter and 2nd Select actions? I am getting error there 🙂
Michal,
Use this code to filter on the Index. I will add it to my article:
item()[‘Index’]
In last Select after copy this expression comes error :
The value of second parameter is of type ‘Object’ but the function ‘substring’ expects a integer or a decimal number.
I am following #1 Remove Invalid Characters Method scenarioCould you paste “Select: array of valid Characters” action expressions? 🙂
ok – my mistake.. sorry for messages.
item()[‘Char’] works good
In the “Select: Array of valid characters” I have item()[‘Char’] and I get this {“N”:””} I only want the N. When I try to do a substring I get an error about ‘substring’ out of range.
Keith,
The output of Select: Array Of Valid Characters should look like this:
[“d”,”a”,”t”,”a”]
I am unsure how you got the result of:
{“N”: “”}
I am using your code in my own flow and that is the first letter brought back from a contract number. I figured it out though I had to modify the text output for only the letter and not all the other characters like the { and the ” marks. The information was very helpful. Thank you
how did you resolve this? having the same issue! THANKS
Hi Matthew,
When i used item()[‘Char’] in Select: Array of Valid Characters
Output shows:
{
“d” : “”
}.
It should look like your output
“d”,
“a,”
Hasham,
When I checked my flow history I can see it correctly output [“d”, “a”…,]. Did you remember to switch the Select action to text value mode?
In the “Select: Array of Valid Characters” you never show/say what to set for the Map field.
What should be in that? It shows the Char output from somewhere but I don’t have that as a selectable option, and I’ve confirmed everything else exactly matches what you show in this.
The missing code: In the Map field for Select:Array of Valid Characters the code is:
You will want to switch to Text mode before you add the code to Map. Then paste in item()[‘Char’]. The screenshot is misleading because when you add this it will be the red ‘fx expression’ format ‘item()’ but once you save it and come bac, it will revert to the screenshot here and change to the purple ‘Char’.
Great job, Matt, as per usual!
I am using the first approach but I am getting an error on select action after filtering the array.
The flow says that the second value should be an integer instead of an object.
Sunil,
The substring function does not belong in the Filter Array step. You are putting it in the wrong place.
Thanks, Matt, for the awesome Power Platform blog site. Your hard work helps countless IT pros around the world!
I’d like to add to the conversation that you can also use the Replace() function to replace characters in a string. Replace() can be nested, so you replace the first character from the string source, then add extra nested replace() functions for the other characters. It might seem like a ton of ugly code, but a single line of code to replace several actions helps keep your flow nice and tidy.
This example removes characters from the variable varString1 by replacing each character with a blank value, ”:
Cheers!
Aaron
Caitlin,
He pasted the value one the page but at a confusing place (filter parameter is between two “Select: Array Of Text String Chars To Validate – Map field – XXX value”).
Check the code for:
Select: Array Of Text String Chars To Validate – Map field – Index value
and lower down
Select: Array Of Text String Chars To Validate – Map field – Char value
When I try to run this, it throws an error that 2 parameters are needed for the indexOf step in the select step. any attempt to fix it causes all of the characters to have an index of -1, like its looking at the whole string of valid characters instead of individually. How did you get indexOf to run with only one string parameter? any help is appreciated. Thanks! :):)
Hi currently using #2 Keep Only Valid Characters Method
When i try to use the following expression in the ‘Select: Array Of Text String Chars To Validate’ step, I get the message ‘The expression is invalid’:
Select: Array Of Text String Chars To Validate
Select: Array Of Text String Chars To Validate – Map field – Index value
All the labelling is correct on the steps so not sure why the expressions aren’t being accepted for this step. Can you please provide some advice 🙂 ?
I couldn’t get the Filter array: Index Does Not Equal -1 (choose a value field) to work. I had to use Greater or Equal 0 instead. (“where”: “@greaterOrEquals(item()[‘Index’],0)”)