0

I would like to replace first 0 with two other digits in AMPscript E.g. 0123012345I would like this to be 98123012345

I've tried substring/replace but it looks for all 0s in the number and does not output expected.

Thanks,

4

Not sure I fully understand your use case, but this will work if you want to trim the 0 just if the string starts with 0 and replace it with 98:

%%[

var @num, @repl

set @num = "0123012345"

if indexOf(@num, "0") == 1 then

  set @repl = Concat("98",Substring(@num,2,Length(@num)))

endif

]%%
number: %%=v(@num)=%%
<br>
replaced: %%=v(@repl)=%%

Above will output:

number: 0123012345
replaced: 98123012345
| improve this answer | |
  • hI @zuzannamj, from my understanding the index of looks for 0 in any position of the string. Is there way to check if the string starts with 0 and set repl variable if it does? – s3002068 Aug 13 at 7:17
  • 2
    That's exactly what I did :-) I've added a condition if indexOf(@num, "0") == 1 which checks if the position of 0 is 1 which means that it's the first character of the string. – zuzannamj Aug 13 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.