Forloop items append to string



My task is to add all the items that I get in the for-loop in to a string.

For example,
1st loop has item: 123
2nd loop has item: 456
3rd loop has item: 678

How to put all into a string like this 123,456,678>???

My long time concern is how to append the variable in the list outside of a forloop.
For example in python, we can do

y = []
for x in xs:

what about uipath?



Hi there,

You can concatenate a string using + or &
For example:
variable = variable + If(variable.Trim<>"",",","") +item

You need an if as shown for the first assignment

However, I recommend possibly trying String.Join()
For example:
variable = String.Join(",", variable, item)

EDIT: also if you already have an array, you can simply use String.Join
String.Join(",", itemarray)



Thanks a lot Clayton. I should have thought of it! I have been trying to use item.ToString + “;” without success. But yours works perfectly! Also, thanks for the reminder about array. My variable is in IEnumberable but I guess it means in array too - works well.

Again, thank you much!


btw, how to remove the first , ??

Based on my scenario, I need to use if statement to filter out some items - so I have to use a for-loop I think. And I am using variable = String.Join(",", variable, item). but , shows as the first character. Thanks!!


Oops. I guess you need to use an If condition so it only joins it if it’s not the first item.

If(variable.Trim="",item, String.Join(",", variable, item) )

That way it just stores item into the variable when it’s the first item since variable is empty, and joins them after that.

The concatenate example I showed above does this too with the if condition.



Hi @ClaytonM,

I got this error when I use If…

I did these:

variable = If(variable.Trim="",item, String.Join(",", variable, item) )
variable = If(variable.Trim="",item, String.Join(",", variable, item) ).toString

But I did assign the variable called variable. It worked fine without the if statement. I am also trying to use the if statement to only add unique values to the string. I tried to do

variable = If(variable.contains(item), variable, String.Join(",", variable, item) )

Get the same error of course - but do you have a better way to only add the distinct item into the string?

Thanks! Looking forward to hearing from you.
edit: still stuck- please help!



Hi. Try adding .ToString to the end, so it converts an empty variable to an empty string.

If(variable.ToString.Trim="",item, String.Join(",", variable, item) )

That error always occurs when you have not assigned anything to the variable.

Another solution you could try is assigning “” to the default value of the variable. This way it has a value stored in it so it won’t be seen as nothing.



Thanks a lot @ClaytonM!