Replace string once?

I’m trying to figure out how to replace a string/ character/ regex once. For example:

  1. How to replace the first “|” with “B” but not the remaining pipe characters?
    |aaaaaaaaaaaaaaaaaaa|aaaaaaaaaaaaaaaaa|aaaaaaaaaaaaaa|

  2. How to replace the first ‘NOW’ with “B” but not the remaining “NOW” strings?
    NOWaaaaaaaaaaaaaaaNOWaaaaaaaaaaaaaaaaaaaNOWaaaaaaaaaaaaaa

One way is to use indexOf and substring rather than regex. I’m pretty sure regex can do this too though, I just can’t remember, but maybe you can do some searches on how to replace the first occurrence.

Here is indexOf substring method:

str.Substring(0,str.IndexOf(keyWord)) + str.Substring(str.IndexOf(keyWord)+keyWord.Length)

where str is your entire text and keyWord is what you are trying to replace. It essentially uses its index to split the text up between the key word you want to replace.

Regards.

Hello @css,

If the chars are in a defined place in the String (ex. positions 1-3) you can use the Substring method to grab them and replace it.

YourString.Replace(YourString.substring(1,3),"")

Otherwise, you can take a look at Regex.Replace method from MSDN.

1 Like

See the attached… Flowchart.xaml (14.7 KB)

Accomplished this by splitting the string and reconstructing through a while loop and an if condition

Hi @mancl0ud, I tried your code in an Assign activity, but it ended up replacing every instance not just one instance.

you can use Clayton’s answers as indexof will return the first occurence, only need to make sure one does exist, or you have an error…

1 Like

Hi @ClaytonM let me know if I’m doing this correctly. The string is vScreen_Capture and I’m trying to just replace the first instance of “|” with “||”. Is this how I would write this in an Assign activity?

vScreenCapture.Substring(0,vScreen_Capture.IndexOf(“|”)) + vScreen_Capture.Substring(vScreen_Capture.IndexOf(“|”)+keyWord.Length)

You need to add “||” after the + so you can concatenate it inbetween.
Like this:
vScreenCapture.Substring(0,vScreen_Capture.IndexOf("|")) + "||" + vScreen_Capture.Substring(vScreen_Capture.IndexOf("|")+"|".Length)

You also needed to change keyWord to “|” on the end so it gets the length, which is just 1. So, technically you can use 1 instead of taking the length, but it was done to try and be more dynamic.

Regards.

3 Likes

Thank you @ClaytonM this worked, would you be able to take a look at my other post, it is different but not completely dissimilar to this one: https://forum.uipath.com/t/find-text-in-string-and-replace-a-part-of-it/183384/7

Hi Mike @css ,

If you want to use the ‘replace’ activity, then it sounds like you are looking for non-greedy regular expressions. Try the following in your examples:

  1. “^.*?(\|).”
  2. “^.*?(NOW).”

Any problems let me know. You can use the substring example Clayton showed you too.

Sorry for the edits… I want to be clear on this one - it would match everything from start of string up to first “|” or “NOW”. In your examples they were the first characters. Say you had something like “aaaaaa|aaaaaaa|sssss” and you wanted to replace only the first pipe (|) with an “X” - you could do this:

Search for: “^(.*?)(|)”
Replace with: “$1x”

Hope that makes sense.

Cheers,

1 Like

Hi @pduffy,how would I implement your solution if I was trying to use a replacement activity and I’m replacing the value of a variable but only want to replace it once?

So the variable would be vVariable1 which has a value of “sqft||Landing”

Are you trying to match either “sqft” or “Landing”, or are you trying to match the literal string “sqft||Landing” ?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.