Replace Activity

Hi all,

I am looking to look at some text, and if it contains a special character ($#@$^%& etc.) replace it with an underscore _

I am trying to use the Replace activity, but am not having success with special characters

Anyone know of a good solution?


Please check below thread for your reference. - Remove special characters from a string - Stack Overflow

You can do this with a regex.replace rather than a simple string.replace.

Make sure to import System.Text.RegularExpressions in the ‘Imports’ pane (near the arguments/variables pane)

Assign YourUpdatedString = Regex.Replace(YourOriginalString,"[" + Regex.Escape("#@^%&") + "]","_")

Put any other special characters you want replaced within the parentheses of regex.escape() above

Can I use this with an if function? It will be looking for the subject of an email, some may contain special characters, some may not.

Also, where do you actually write this in? I’m only familiar with using UiPath activities so not sure where something like this would actually go?

No need to use an if statement. If no special characters are in the input string (I called it YourOriginalString) then there is nothing to replace and your output string (I called it YourUpdatedString) will stay exactly the same.

As it’s written above, it goes into an assign activity. YourUpdatedString is a string variable - this is a newly created variable which is the original string, but with special characters replaced with underscores. YourOriginalString is also a string variable - this is the original string that may contain special characters. Those were variable names i made up, you can name them anything you’d like.

Ok i’ll give this a shot thank you!