Replace only the last instance of a comma in a string

Hi!

How would I go about replacing only the last comma in a string and changing it to “and”?

For example:

Neebs, Appsro, Thick, Dora, Simon

I would want to replace only the comma between “Dora” and “Simon” and change it to the word “and”, so the string becomes:

Neebs, Appsro, Thick, Dora and Simon.

Appreciate any feedback, thanks!

Hi @Sheri,

You should be able to use regex replacing to do this!
See below:


In UiPath, you would need to have the Regular Expressions namespace/import and could then use Assign as such:
strVariable = Regex.Replace(yourString, “(?:,)([^,]+)$”, " and\1").ToString
Note that there is an extra space before the and in the replace.

Let me know if you have any more questions :slight_smile:

Hi,

You can get the position of last comma with LastIndexOf method.

intIndex = strData.LastIndexOf(",")

Then, you can remove it and insert " and "

strData = strData.Remove(intIndex,1).Insert(intIndex," and ")

Regards,

Yoichi

2 Likes

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