Top
Photo by Brett Jordan on Unsplash

Replace special characters using Power Automate


Have you ever faced it? A system, that accepts just international characters while users input local ones, like Polish? Or Spanish? Well, Power Automate is here to help them get replaced!

This can be done using just two steps, although the replacement itself may be not very efficient when used to handle really long texts. I am using it to replace short strings, like titles or product names. If you plan to use it for a long text, just observe how much time would it need.

Special characters replacement Power Automate flow

Replacement matrix

First thing is to create a replacement matrix. This can be done to replace any character to something else or to nothing. Do it using a JSON object and a “Compose” action. In my example you can find all Polish special characters:

{
  "ą": "a",
  "Ą": "A",
  "ę": "e",
  "Ę": "E",
  "ć": "c",
  "Ć": "C",
  "ł": "l",
  "Ł": "L",
  "ó": "o",
  "Ó": "O",
  "ś": "s",
  "Ś": "S",
  "ź": "z",
  "Ź": "Z",
  "ż": "z",
  "Ż": "Z",
  "ń": "n",
  "Ń": "N"
}

Replace characters

Next, you need to use the “Select” action, to get a range of every single character, then to iterate through characters, and to lookup their presence in the replacement matrix:

Use Select to replace characters
range(0, length({TEXT TO CHANGE}))

“Range” expression returns an array of integers indicating start of each character in the string. And then the map:

if(not(empty(outputs('Replacement_matrix')?[substring({TEXT TO CHANGE}, item(), 1)])), outputs('Replacement_matrix')[substring({TEXT TO CHANGE}, item(), 1)], substring({TEXT TO CHANGE}, item(), 1)) 

Map part of the action looks up each not empty character from the string, that starts on value from the array of strings and has 1 character length, if is present in the replacement matrix. If yes – it’s being replaced.

Result

What the “Select” action returns is an array of each single character from the input string. Final step then is to simply join the array into a string. Use “Compose” action again and the following expression:

join(body('Replace_characters'), '')

Let’s see it in action

First I passed a short string: “Zażółć gęślą jaźń” to test the solution. It took the whole process around 100ms to run:

Result of flow.

I then used chatGPT to generate me a text using the following query: “Generate one paragraph having 20 sentences each having at least 10 words of random text in Polish.“. The result was quite a long text:

Random Polish text generated by chatGPT.

Process needed around 200ms to complete. I asked chatGPT to generate me a novel about Warsaw, that has at least 500 words. Needless to say, it’s quite an amount of text 🙂 Still, the time needed to complete the action was really short: about 330ms!

Let me know how you are using this solution and how much time it needs on certain cases.


Tomasz Poszytek

Hi, I am Tomasz. I am expert in the field of process automation and business solutions' building using Power Platform. I am Microsoft MVP and Nintex vTE.

No Comments

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.