WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bettman on May 08, 2014, 11:32:22 AM

Title: String Manipulation
Post by: bettman on May 08, 2014, 11:32:22 AM
Greetings,

Could someone tell me how to replace a known substring at the end of a base string? For explanation purposes, the substring at the end of the base string will always be xyz; I would like to replace that with 123. The problem is there are many occurrences of xyz in the base string; I only want to replace the one at the end of the base string.

Any help would be appreciated.
Title: Re: String Manipulation
Post by: Deana on May 08, 2014, 12:17:49 PM
Quote from: bettman on May 08, 2014, 11:32:22 AM
Greetings,

Could someone tell me how to replace a known substring at the end of a base string? For explanation purposes, the substring at the end of the base string will always be xyz; I would like to replace that with 123. The problem is there are many occurrences of xyz in the base string; I only want to replace the one at the end of the base string.

Any help would be appreciated.

I recommend using StrSub to extract everything up to the last three characters then append the new three characters using StrCat or Colon concatenation.

For example:
Code (winbatch) Select

; Replace last three characters in a string
strA = 'xyzxyzyxz'
strPart1 = StrSub( strA, 1, StrLen(strA)-3 )
strB =  strPart1 : '123'
Pause(strA, StrB)
Exit
Title: Re: String Manipulation
Post by: kdmoyers on May 09, 2014, 08:23:17 AM
My take on it:
Code (winbatch) Select
a = "asdwerasdrtyasd" ; orig
b = "asd"             ; look for
c = "1234"            ; replace with

p = strindex(a,b,0,@backscan)
if p == strlen(a)-strlen(b)+1 ; is there?
  a = strsub(a,1,strlen(a)-strlen(b)):c
endif

message("result",a)