Power Automate: Remove Characters From A String
Power Automate: Remove Characters From A String Posted by - Matthew Devaney on - March 17, 2024 22 Comments A Power Automate flow can unexpectedly fail to du...
- Published
- Reading time
- 6 min read
Before you start
Is this guide for you?
- Best entry point
- Power Automate
- Time investment
- 6 min read
Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/power-automate-remove-characters-from-a-string/. Author: Matthew Devaney.
Power Automate: Remove Characters From A String Posted by - Matthew Devaney on - March 17, 2024 22 Comments
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
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
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 ~`!@#$%^&*()+|{}[] <> /?;:., Code language: HTML, XML ( xml )
Compose: Text String To Validate – Inputs field data$batch! -2024.03 .02 Code language: PHP ( php )
Select: Array Of Text String Chars To Validate – From field
range( 0 ,length(outputs( 'Compose:_Text_String_To_Validate' ))) Code language: JavaScript ( javascript )
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 )) Code language: JavaScript ( javascript )
Select: Array Of Text String Chars To Validate – Map field – Char value
substring(outputs( 'Compose:_Text_String_To_Validate' ),item(), 1 ) Code language: JavaScript ( javascript )
Filter array – Index Equals -1 (choose a value field) item () [ 'Index' ] Code language: CSS ( css )
Compose: Valid Text String – Inputs field
join(body( 'Select:_Array_Of_Valid_Characters' ), '' ) Code language: JavaScript ( javascript )
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 Code language: PHP ( php )
Select: Array Of Text String Chars To Validate – From field
range( 0 ,length(outputs( 'Compose:_Text_String_To_Validate' ))) Code language: JavaScript ( javascript )
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 )) Code language: JavaScript ( javascript )
Select: Array Of Text String Chars To Validate – Map field – Char value
substring(outputs( 'Compose:_Text_String_To_Validate' ),item(), 1 ) Code language: JavaScript ( javascript )
Filter array: Index Does Not Equal -1 (choose a value field) item () [ 'Index' ] Code language: CSS ( css )
Compose: Valid Text String – Inputs field
join(body( 'Select:_Array_Of_Valid_Characters' ), '' ) Code language: JavaScript ( javascript )
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 Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up 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 Power Automate
Save Docusign Document To SharePoint With Power Automate
Tagged
Power Automate
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related questions
Related comparisons
Next action