String Manipulation

Started by bettman, May 08, 2014, 11:32:22 AM

Previous topic - Next topic

bettman

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.

Deana

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
Deana F.
Technical Support
Wilson WindowWare Inc.

kdmoyers

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)
The mind is everything; What you think, you become.