Replace special characters with spaces in a string
Moderators: dbzTHEdinosauer, Moderator Group
Replace special characters with spaces in a string
Hi Folks,
Can anyone please let me know how can i replace the special characters in a string with spaces?
Ex: If i have a string 'ABC@123#d_efg)'
then it should give me 'ABC 123 d efg '
By using INSPECT we can do it provided if we know the special characters in advance but in my case i can't do it either because there are numerous special characters in my project and i can't use INSPECT for every special character and for every time.
so,I used the below logic to fix this but this seems not working.
77 ws-add pic x(50).
77 ws-var pic x(50).
move '1!2@cd#ef$gh%ij^klmn*&' to ws-add
*'1 2 cd ef gh ij klmn ' is my desired output
move ws-add to ws-var
PERFORM para-1 VARYING ws-index FROM 1 BY 1
UNTIL ws-index > 50
para-1:
if ws-var(ws-index:1) is numeric
move ws-var(ws-index:1) to ws-add(ws-index:1)
else if ws-var(ws-index:1) is alphabetic
move ws-var(ws-index:1) to ws-add(ws-index:1)
else
move spaces to ws-add(ws-index:1).
Can anyone throw some light on this ?
Thanks & Regards,
AAkheel.
Can anyone please let me know how can i replace the special characters in a string with spaces?
Ex: If i have a string 'ABC@123#d_efg)'
then it should give me 'ABC 123 d efg '
By using INSPECT we can do it provided if we know the special characters in advance but in my case i can't do it either because there are numerous special characters in my project and i can't use INSPECT for every special character and for every time.
so,I used the below logic to fix this but this seems not working.
77 ws-add pic x(50).
77 ws-var pic x(50).
move '1!2@cd#ef$gh%ij^klmn*&' to ws-add
*'1 2 cd ef gh ij klmn ' is my desired output
move ws-add to ws-var
PERFORM para-1 VARYING ws-index FROM 1 BY 1
UNTIL ws-index > 50
para-1:
if ws-var(ws-index:1) is numeric
move ws-var(ws-index:1) to ws-add(ws-index:1)
else if ws-var(ws-index:1) is alphabetic
move ws-var(ws-index:1) to ws-add(ws-index:1)
else
move spaces to ws-add(ws-index:1).
Can anyone throw some light on this ?
Thanks & Regards,
AAkheel.
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
Hi Folks,
Sorry for incomplete post.
But as i mentioned, using Refernce Modification for every Feild,
like if it is of x(250),then it has to check for 250 times and it will make it very tedious and hence the performance of the program degrades.
So, i request to provide me an optimized way to fix this thing.
Thanks & Regards,
Aakheel.
Sorry for incomplete post.
But as i mentioned, using Refernce Modification for every Feild,
like if it is of x(250),then it has to check for 250 times and it will make it very tedious and hence the performance of the program degrades.
So, i request to provide me an optimized way to fix this thing.
Thanks & Regards,
Aakheel.
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
Hi William,
Actually its an address feild to which it receives from another system so there might be some junk values like 'square'.*/.... for which i need to remove it and fill it with spaces(Only Numerics and Alphabetics allowed).
By performing the above code it satisfies but it will degrade the performance.
For example, if i have to generate a memo with x(1000), then it will check for all the 1000 letters present in the feild which i feel is not the better way and hence finding for an alternate way to optimize it.
Any help would be appretiable.
Actually its an address feild to which it receives from another system so there might be some junk values like 'square'.*/.... for which i need to remove it and fill it with spaces(Only Numerics and Alphabetics allowed).
By performing the above code it satisfies but it will degrade the performance.
For example, if i have to generate a memo with x(1000), then it will check for all the 1000 letters present in the feild which i feel is not the better way and hence finding for an alternate way to optimize it.
Any help would be appretiable.
-
- Member
- Posts: 12
- Joined: Tue Jan 14, 2014 1:20 pm
- Contact:
If you have a list of possible special characters you can use INSPECT with REPLACING.
INSPECT source-string REPLACING ALL '#' BY ' '
ALL '_' BY ' '
INSPECT source-string REPLACING ALL '#' BY ' '
ALL '_' BY ' '
Payilagam Chennai
www.payilagam.com
www.payilagam.com
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
This, then, is something to be done at the time the data is captured. You do it once, it is done.
If there is some absurd reason that it can't be done at data-capture, then how does the data get to your system? Is it stored on the other system then passed to you every time? Or is it passed to you once, so you have your "capture" and you do it at that point, once only.
If it is passed every time, you (your boss) should insist that the system which passes it does the validation. Then, when they realise what a pain that is, they will do it themselves at the point of capture.
Don't look at REPLACING, look at INSPECT .... CONVERTING, but you'd have to convert every unwanted value.
If you can't get good data given to you, then you have to look at making your code better. Although I don't like destorying the contents of a field, since you are doing it anyway, why have two copies of the field?
Also, since you only want to space-out the values you don't want, you doin't need to MOVE the bytes when you discover they are OK. Since you are looking at text, and fields will allow for maximum size, you should identify trailing space and stop your tests when you reach that. Do that with the same looping, but starting from the end of the field and going "backwards" until you get the first non-blank.
That'll speed your code up, but even more so if you don't have to do it because it is done at the correct point...
If there is some absurd reason that it can't be done at data-capture, then how does the data get to your system? Is it stored on the other system then passed to you every time? Or is it passed to you once, so you have your "capture" and you do it at that point, once only.
If it is passed every time, you (your boss) should insist that the system which passes it does the validation. Then, when they realise what a pain that is, they will do it themselves at the point of capture.
Don't look at REPLACING, look at INSPECT .... CONVERTING, but you'd have to convert every unwanted value.
If you can't get good data given to you, then you have to look at making your code better. Although I don't like destorying the contents of a field, since you are doing it anyway, why have two copies of the field?
Also, since you only want to space-out the values you don't want, you doin't need to MOVE the bytes when you discover they are OK. Since you are looking at text, and fields will allow for maximum size, you should identify trailing space and stop your tests when you reach that. Do that with the same looping, but starting from the end of the field and going "backwards" until you get the first non-blank.
That'll speed your code up, but even more so if you don't have to do it because it is done at the correct point...
One thing you might do is look for the valid characters rather than the invalid ones.
Then define an 88 level that includes all valid codes. A simple IF will do for each position (reference modification to move across the input data). When valid, leave alone otherwise move space.
What testing have yuou done to verify how much resource is needed.
It really doesn't matter as the data Must be correct.
Then define an 88 level that includes all valid codes. A simple IF will do for each position (reference modification to move across the input data). When valid, leave alone otherwise move space.
What testing have yuou done to verify how much resource is needed.
It really doesn't matter as the data Must be correct.
Have a good one
Below condition will help to remove the unwanted values from field, if we receives the value other than the ALPHABETIC and NUMERIC below code will replaces the JUNK or unwanted values with "spaces".
01 STREET1-ADDRESS X(25)
01 ADDR1 REDEFINES STREET1-ADDRESS OCCURS 25 TIMES
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE
OUTPUT OUTPUT-FILE,
MOVE INPUT-ADDR1 TO STREET1-ADDRESS
IF (ADDR1 (SUB1) ALPHABETIC) OR
(ADDR1 (SUB1) NUMERIC)
NEXT SENTENCE
ELSE
MOVE SPACES TO ADDR1 (SUB1)
END-IF.
01 STREET1-ADDRESS X(25)
01 ADDR1 REDEFINES STREET1-ADDRESS OCCURS 25 TIMES
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE
OUTPUT OUTPUT-FILE,
MOVE INPUT-ADDR1 TO STREET1-ADDRESS
IF (ADDR1 (SUB1) ALPHABETIC) OR
(ADDR1 (SUB1) NUMERIC)
NEXT SENTENCE
ELSE
MOVE SPACES TO ADDR1 (SUB1)
END-IF.
Thanks
Devendar
Devendar
FREE TUTORIALS
Tutorials
Free tutorials from mainframegurukul
- JCL Tutorial
Covers all important JCL concepts. - Cobol Tutorial
This tutorials covers all Cobol Topics from STRING to COMP-3. - DB2 Tutorial
DB2 Tutorial focuses on DB2 COBOL Programming. - SORT Tutorial
This Tutorial covers all important aspects of DFSORT with examples - CICS Tutorial
This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions
- Cobol Interview Questions
50+ Interview Questions - JCL Interview Questions
50+ Interview Questions - DB2 Interview Questions
100+ Interview Questions - CICS Interview Questions
70+ Interview Questions - VSAM Interview Questions
27 Interview Questions
Other References
Mainframe Tools and others
- XPEDITER Reference
Explains how we can debug a program - FILEAID Reference
Explains how to browse , edit and delete datasets - Change Man Reference
Quick Start tutorial on Changeman - Abend Reference
Important Abend codes explained - FaceBook Page
MainframeGurukul FaceBook Page - LinkedIn Page
MainframeGurkul Linkedin Page