Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

%SCAN - how to remove special characters

2,110 views
Skip to first unread message

Prad

unread,
Jun 15, 2006, 6:27:20 AM6/15/06
to
Hi there,

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.

fifin

unread,
Jun 15, 2006, 10:06:57 AM6/15/06
to
Hi,

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

Steve Richter

unread,
Jun 15, 2006, 11:14:57 AM6/15/06
to

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

James Perkins

unread,
Jun 15, 2006, 11:17:01 AM6/15/06
to

> "Prad" <netpa...@hotmail.com> wrote in message
> news:1150367240.5...@i40g2000cwc.googlegroups.com...

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

Birgitt...@lp-gmbh.com

unread,
Jun 15, 2006, 12:40:38 PM6/15/06
to
Hi,

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

0 new messages