How to append more text into a string variable

hi

I want to append data into a variable on a specifik place.

varibale1 = Michael jackson, 29081958 (256), Gary (75), USA (13)

Now after (256) and before “,” I want to add “Space (King of Pop)”

like = Michael jackson, 29081958 (256) (King of Pop), Gary (75), USA (13)

How can use split or append together? OR do i need to use something else?

Hi @Latif,

Assign Activity:

variable1 = 
variable1.Insert(variable1.IndexOf("(256)") + 4, " (King of Pop)")

How it works: Finds end of “(256)” and inserts your text right before comma. no split needed.

Test first in Debug mode

@Latif

Is your string going to be same or dynamic? if same then go with the IndexOf option to get the exact index at which you have to append your value.

If string is going to be dynamic, you will have to find a pattern and then using RegEx you can replace or append the desired value.

As its differet from person to person so it will never be same.. (256) that was just an example.

the whole variable will contain data rekated to the spcifik person. so i need something with finding first bracket og last part for first brackets and then replace or add

Hi @Latif

Try like below Example:

System.Text.RegularExpressions.Regex.Replace(variable1, @“)(?=,)”, “) (King of Pop)”)

This finds ) before the first comma and appends your text. For dynamic text, replace “King of Pop” with your variable. No need to split; regex is best for this case.

If helpful, mark as solution. Happy automation with UiPath

It gives error..

@Latif

Try this RegeX

variable1 = System.Text.RegularExpressions.Regex.Replace(variable1, "(d+)", "$0 (King of Pop)")

sorry but this is not write as i read..

I want to find the specifik place and then append the text. so this will never work.

Two Assign Activities (works for ANY person data):

*1st Assign:

pos = variable1.IndexOf("(")

*2nd Assign:

variable1 = variable1.Insert(pos + variable1.IndexOf(")", pos) + 1, " (King of Pop)")

is Pos as String?

as it still gives Error

@Latif

No, pos must be an Integer variable!

Create variable pos as type Int32 (not String).


varindex is int32.

@Latif

Hey! Multiple Assign can’t reference variables created in the same activity. That’s your error!

Use 2 SEPARATE Assign activities instead

**1st Assign Activity:**
To: `pos` (Int32 variable)
Value: `variable1.IndexOf("(")`

**2nd Assign Activity:** 
To: `variable1` (String variable) 
Value: `variable1.Insert(pos + variable1.IndexOf(")", pos) + 1, " (King of Pop)")`

Why Multiple Assign failed: It evaluates ALL lines simultaneously, so pos doesn’t exist yet in the 2nd line

Now it gives another issue

Its working now.. Index value needed End brackets and not start brackets.
Than it solved the issue.

@Latif

The error happens because IndexOf(“)”) returns -1 (not found), making startIndex negative.

Assign Activities:

**1st Assign:** posOpen = variable1.IndexOf("(")
**2nd Assign:** posClose = If(posOpen > -1, variable1.IndexOf(")", posOpen), -1)
**3rd Assign:** If(posClose > -1, variable1 = variable1.Insert(posClose + 1, " (King of Pop)"), "")

Your string length 52 means no ) after position 57 → fixed now.

I did like this.

Assign [Activities]

**1st Assign:** varIndex = variable1.IndexOf(")")
**2nd Assign:** varName = varName.Insert(varName.indexOf(")", varIndex) + 1, " (King of Pop)")

Result
image

Is this still a correct way?

Yes, its correct and better

This should not be a string variable. You should be using a variable that is datatype IEnumerable(of String) and set its default to New List(Of String). Then you can do…

Assign yourVarName = yourVarName.Concat({“New value to add to the list”})

That will add a value to the list.

Or you can do

Assign yourVarName = yourVarName.Concat({“New value to add”,“Another new value”,“Third new value”})

To add more than one value to the list.

Then you can do sorting and things on the list to change the order.

Wouldn’t it be most simplistic to just use split if the structure is always the second comma?

Assign var1 = “Michael jackson, 29081958 (256), Gary (75), USA (13)”

Assign var2 = “ (King of Pop)”

Assign var1 =
var1.Split(","c)(0) & “, " &
var1.Split(”,"c)(1) & var2 & “, " &
var1.Split(”,"c)(2) & “, " &
var1.Split(”,"c)(3)

And if this modification is to be used for more variables:

(pic from Studio v 2025+)