cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
cfebvre
Advocate V
Advocate V

Best method to find and replace one set of characters for another set of characters

Hi All,

 

Does anyone have a better method to replace one set of invalid characters in a string, with a set of valid characters? The only way I've found so far is to nest replace in an expression:

replace(replace(replace('TexttoReplace', 'a','z'), 'b','y'),'c','x')

 

In my scenario, I need to replace curly quotes with straight quotes, a variety of accented letters with the equivalent non-accented letter (eg: é with e), and so forth. All told, there is probably 30 or more characters I need to replace - and this list will likely expand as we encounter new letters that final final javascript file does not like (a .jsx file is the the file that is created at the end of my flow, hence why I need to strip these characters).

 

In my experimentation, I've managed to get this far:

 

My text string is 'abcdefghi'

 

My aim is to get a result string of 'zyxwvutsr'

 

1) I set up 3 arrays of characters.

  • The first array is my set of invalid characters (Inv) (abcdefghi)
  • The second array is the set of valid characters to replace it with (Val) (zyxwvutsr)
  • The Third array is a set of place numbers that reference a specific Invalid character and the valid character to replace it with. '0' refers to a and z, '1' refers to b and y, '2' refers to c and x, etc).

Here are my expressions for both Invalid array and Valid Array.Here are my expressions for both Invalid array and Valid Array.

 

createArray('a','b','c','d','e','f','g','h','i')
createArray('z','y','x','w','v','u','t','s','r')

 

Here is my array for the Character Place in the above arrays, and the text string I am to convert.Here is my array for the Character Place in the above arrays, and the text string I am to convert.

 

createArray('0','1','2','3','5','6','7','8')

2) After I have my arrays set up, and I have the text string I need to convert, the next step is to 'Apply to Each' for each number in the Place Array. Annoyingly, I then have to convert the number (which flow recognises as a string), into an integer.

 

Lastly, I have a replace expression that grabs the original Text string, finds the Invalid character for each iteration of the 'Apply to Each', and replaces that specific character.

 

This section grabs the number for the Place array, converts it into an integer, then I plug the integer into my replace expression.This section grabs the number for the Place array, converts it into an integer, then I plug the integer into my replace expression.

 

int(outputs('GetPlace'))
replace(outputs('Text'), variables('Inv')[outputs('ConvertToInteger')], variables('Val')[outputs('ConvertToInteger')])

 

Now, obviously, the result I'm getting replaces the correct characters, but it only replaces 1 character in each 'apply to each' result, which is not what I want.

 

Results:

zbcdefghi

aycdefghi

abxdefghi

...etc

 

How would I get one result back, that switch all the characters the characters in my string: abcdefghi -> zyxwvutsr ?

 

Thanks in advance if anyone has some ideas.

 

Chris

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Paulie78
Super User III
Super User III

I don't know if this is the best method, but certainly works. https://ibb.co/9NbVw76

 

replaceChars.png

Explanation

First an array is created in a compose step which specifies the character replacements to perform, the code is:

 

[
 {
  "invalidChar": "a",
  "replacementChar": "z" 
 },
 {
  "invalidChar": "b",
  "replacementChar": "y" 
 },
 {
  "invalidChar": "c",
  "replacementChar": "x" 
 },
 {
  "invalidChar": "d",
  "replacementChar": "w" 
 },
 {
  "invalidChar": "e",
  "replacementChar": "v" 
 },
 {
  "invalidChar": "f",
  "replacementChar": "u" 
 },
 {
  "invalidChar": "g",
  "replacementChar": "s" 
 },
 {
  "invalidChar": "h",
  "replacementChar": "t" 
 },
 {
  "invalidChar": "i",
  "replacementChar": "r" 
 }
]

 

Next a string variable is created and initialized with the text containing the invalid characters.

Then an apply to each loop is created which loops around each element of the replaceChars array. The characters are replaced one by one in each iteration of the loop with the expression:

 

replace(variables('text'), item()['invalidChar'], item()['replacementChar'])

 

After the replacement is performed the variable is updated with the set variable action, ready for the next loop. The final unrequired step in my flow is a compose which simply outputs the value of the variable after the loop has completed and shows the final result.

replaceCharsResult.png

See how you get on, it should do what you want.

View solution in original post

2 REPLIES 2
Paulie78
Super User III
Super User III

I don't know if this is the best method, but certainly works. https://ibb.co/9NbVw76

 

replaceChars.png

Explanation

First an array is created in a compose step which specifies the character replacements to perform, the code is:

 

[
 {
  "invalidChar": "a",
  "replacementChar": "z" 
 },
 {
  "invalidChar": "b",
  "replacementChar": "y" 
 },
 {
  "invalidChar": "c",
  "replacementChar": "x" 
 },
 {
  "invalidChar": "d",
  "replacementChar": "w" 
 },
 {
  "invalidChar": "e",
  "replacementChar": "v" 
 },
 {
  "invalidChar": "f",
  "replacementChar": "u" 
 },
 {
  "invalidChar": "g",
  "replacementChar": "s" 
 },
 {
  "invalidChar": "h",
  "replacementChar": "t" 
 },
 {
  "invalidChar": "i",
  "replacementChar": "r" 
 }
]

 

Next a string variable is created and initialized with the text containing the invalid characters.

Then an apply to each loop is created which loops around each element of the replaceChars array. The characters are replaced one by one in each iteration of the loop with the expression:

 

replace(variables('text'), item()['invalidChar'], item()['replacementChar'])

 

After the replacement is performed the variable is updated with the set variable action, ready for the next loop. The final unrequired step in my flow is a compose which simply outputs the value of the variable after the loop has completed and shows the final result.

replaceCharsResult.png

See how you get on, it should do what you want.

View solution in original post

@Paulie78  Absolutely fantastic. That's exactly what I'm after thank you.

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Top Solution Authors
Users online (1,125)