I am looking for a better way in removing special characters like
('!@#$%¢&*()_-<>?/:;"-) from my string. Right now I use %SCAN to look
for the particular character and removing it. Is there any other way
for me to scan the string and remove all the special characters in a
single go.
Please find below the logic that i am currently using to remove ('-')
Eval $Pos = %Scan('-' :Zipcd)
If $Pos = 0
Movel Zipcd Zcdemi
Else
Eval $LenZip = %Len(Zipcd)
Eval $Zipcode = %Subst(Zipcd:1 :($Pos-1)) +
%Subst(Zipcd : ($Pos + 1):($LenZip-$Pos))
Movel $Zipcode Zcdemi
Thanks in advance.
Prad.
maybe better solution is using xlate command :
C '/x,':' ' xlate InStr ResultStr
This command replace characters before ":" in factor 1 by characters
after ":" in factor 1 (positionally). Or it's very usefull to
do this :
D BIG S 40
D SMALL S 40
.
.
.
C eval SMALL=
C 'abcdefghijklmnopqrstuvwxyz'
C eval BIG=
C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.
.
C BIG:SMALL xlate String String
when String='HALLO", after xlate it'll be 'hallo'.
Miki
you could write a sub procedure:
** --------------------- fScanReplaceAll ---------------------
** Rtns = fScanReplaceAll( InString: FindString: RplString )
pfScanReplaceAll b export
dfScanReplaceAll pi 2000a varying
d InString 2000a const varying
d InFind 2000a const varying
d InReplace 2000a const varying
d news s 2000a varying
d Bx s 10i 0
d Fx s 10i 0
/free
news = InString ;
if %len(InFind) > 0
and %len(news) > 0 ;
bx = 1 ;
dou fx = 0
or (bx + %len(InFind) - 1) > %len(news) ;
fx = %scan( InFind: news: bx ) ;
if Fx > 0 ;
news = %replace( InReplace:
news:
Fx:
%len(InFind)) ;
bx = fx + %len(InReplace) ;
endif ;
enddo ;
endif ;
return news ;
/end-free
p e
You could always create a subprocedure and use an array.
D proc1 PR
D s 15a
...
...
D proc1 PI
D s 15a
*
D symArray ds
D arr 1 20 dim(20)
D arr1 1 1 inz('!')
D arr2 2 2 inz('@')
D arr3 3 3 inz('#')
D arr4 4 4 inz('$')
....
*
D i s 10i 0
D x s 10i 0
D lenT s 10i 0
D lenF s 10i 0
D s2 s 50a
D msgKey s 4a
D rplChar c const('RPL')
....
/free
s2 = s;
lenF = %len(arr(i));
lenT = %len(rplChar);
if (lenT = 0);
lenT = 1;
endif;
for i = 1 by 1 to %len(symArray);
x = %scan(arr(i):s2);
dou x = 0;
if x > 0;
s2 = %subst(s2:1:(x - 1)) +
%subst(%replace(rplChar:s2:x:lenT): +
x:lenT) +
%subst(s2:(x + lenF));
x = %scan(arr(i):s2:(x + lenT));
endif;
enddo;
endfor;
/end-free
You could always put the replacement character in an array for something as
well.
I hope this helps.
James R. Perkins
if embedded SQL would be an option for you and if you are already on
release V5R3M0, you could try the following statement:
C/EXEC SQL :NewString = Replace(Translate (:OldString, '!"§$%&/()=',
' ??????????'): '?': '')
C/End-Exec
In the statement above, first your special signs are converted into ?
and after all ? get removed.
Be aware I could not try it out, because I have no access to an iSeries
in this moment.
Birgitta