Power Automate: Remove Characters From A String

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 AutomateFlow Expressions To Remove Characters From A StringRun The Flow To Remove Characters From A StringUnderstanding How The Flow To Remove Characters Works

#2 Keep Only Valid Characters Method
• Keep Only Valid Characters In A Text StringFlow Expressions To Keep Only Valid Characters In A Text StringRun The Flow To Keep Only Valid Characters In A StringExplaining 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.





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.

Matthew Devaney

Subscribe
Notify of
guest

11 Comments
Oldest
Newest
Inline Feedbacks
View all comments
michal
michal
30 days ago

Could You provide expressions inside Filter and 2nd Select actions? I am getting error there 🙂

michal
michal
29 days ago

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.

michal
michal
29 days ago
Reply to  michal

I am following #1 Remove Invalid Characters Method scenarioCould you paste “Select: array of valid Characters” action expressions? 🙂

michal
michal
29 days ago
Reply to  michal

ok – my mistake.. sorry for messages.
item()[‘Char’] works good

Keith
Keith
28 days ago
Reply to  michal

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.

Last edited 28 days ago by Keith
Keith
Keith
24 days ago

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

Hasham
Hasham
25 days ago

Hi Matthew,

When i used item()[‘Char’] in Select: Array of Valid Characters
Output shows:
{
“d” : “”
}.
It should look like your output
“d”,
“a,”

Last edited 25 days ago by Hasham
Michael
Michael
21 days ago

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.