-1

I need to replace all instances of a sub-string in a string

I am using

A1.toString().replace(to_replace,replace_with)

But it is only replacing the first instance

Ex

to_replace = ",@"
replace_with = "|"

I get for a cell value

a,@b,@c,@d  >>  a|b,@c,@d 

I need

a,@b,@c,@d >> a|b|c|d

I can not figure out if their is a setting to replace all not just the first instance

Thanks

Jersy One
  • 28
  • 5

1 Answers1

0

You can use this trick to do it:

A1.toString().split(to_replace).join(replace_with) 

this will divide your string to an array without substring to replace, and then will build it again to a new string with the new substring, acting like a replace all function

regev avraham
  • 1,102
  • 1
  • 9
  • 24