Author Topic: Replace 1 character in a text value  (Read 1994 times)

DeeM77

  • Guest
Replace 1 character in a text value
« on: December 10, 2017, 04:15:55 AM »
Hi,

i'm trying replace a character in a text value. I want the 3nd character to be replaced with '5' but without altering all the other characters.
So when I start the Value is 111 and the desired output is 151. Now it replaces every character after the 2nd character.

Code: [Select]
Set Text [module1] to '111'
Write '[Blue] {TXT:module1}' to log
Set Text [module1] to '{EXP:SUBSTRING('{TXT:module1}',2,1)}5'
Write '[Blue] {TXT:module1}' to log


Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Replace 1 character in a text value
« Reply #1 on: December 10, 2017, 09:47:47 AM »
Here's one way to do it:
Code: [Select]
Set Text [module1] to '11111111111'
Set Text [module1] to '{TXTSUBSTR:module1:0:2}5{TXTSUBSTR:module1:3:}'
Write '[Blue] {TXT:module1}' to log


Code: [Select]
{TXTSUBSTR:module1:0:2}Returns the first two characters of your text variable(From position 0, which is the first character, and 2 characters long, so character position 0 and 1).

Code: [Select]
5Is a literal character "5", which you could replace with a token if you need it to be variable

Code: [Select]
{TXTSUBSTR:module1:3:}Returns the remaining characters of your text variable(from position 3, which is actually the fourth character position, to blank, or "Not Set", which means it'll keep going 'till it runs out of characters in your variable to return).