View previous topic :: View next topic
|
Author |
Message |
harishch_ch
New User
Joined: 07 Jan 2008 Posts: 27 Location: bangalore
|
|
|
|
Hi,
I want to replace a string "xyz" with "abc" using translate and i proceeded in the below manner:
N = '343x324xyz'
TRANSLATE(N, 'abc','xyz')
o/p is: 343a324abc
I want only 'xyz' to be replaced with 'abc'.
Where as Translate is done at Character Level. i.e., 'x' replaced by 'a'
Required o/p : 343x324abc
It this possible in PL/i? and How?
Regards,
Harish |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Code: |
IF INDEX(N, 'abc') > 0 THEN N = SUBSTR(N, 1, INDEX(N,'abc')-1) !! SUBSTR(N, INDEX(N,'abc')+3) ; |
Untested but I do similar things in SAS all the time. If you can have more than one occurrence of the pattern in the string, you may need a DO WHILE loop. |
|
Back to top |
|
|
PeD
Active User
Joined: 26 Nov 2005 Posts: 459 Location: Belgium
|
|
|
|
??
Code: |
IF INDEX(N, 'xyz') > 0 THEN N = SUBSTR(N, 1, INDEX(N,'xyz')-1) !! 'abc'!!SUBSTR(N, INDEX(N,'xyz')+3) ; |
|
|
Back to top |
|
|
donateeye Warnings : 2 New User
Joined: 01 Jun 2007 Posts: 62 Location: chennai
|
|
|
|
substr(N,8,3)='abc';
this could do the trick for you.... |
|
Back to top |
|
|
PeD
Active User
Joined: 26 Nov 2005 Posts: 459 Location: Belgium
|
|
|
|
If the string to be replaced is always at the same place.... |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
My assumption is that the string to be replaced can be located anywhere in the string ... hence the code. |
|
Back to top |
|
|
harishch_ch
New User
Joined: 07 Jan 2008 Posts: 27 Location: bangalore
|
|
|
|
Thanks all.. the above works fine! |
|
Back to top |
|
|
|