Standard way to remove spaces from input in cobol?
Solution 1
One would hope for a more elegant way of simply trimming text strings but this is pretty much the standard solution... The trimming part is done in the SHOW-TEXT paragraph.
*************************************
* TRIM A STRING... THE HARD WAY...
*************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. TESTX.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 USER-INPUT PIC X(30).
01 I PIC S9(4) BINARY.
PROCEDURE DIVISION.
MOVE SPACES TO USER-INPUT
PERFORM SHOW-TEXT
MOVE ' A B C' TO USER-INPUT
PERFORM SHOW-TEXT
MOVE 'USE ALL 30 CHARACTERS -------X' TO USER-INPUT
PERFORM SHOW-TEXT
GOBACK
.
SHOW-TEXT.
PERFORM VARYING I FROM LENGTH OF USER-INPUT BY -1
UNTIL I LESS THAN 1 OR USER-INPUT(I:1) NOT = ' '
END-PERFORM
IF I > ZERO
DISPLAY USER-INPUT(1:I) '@ OTHER STUFF'
ELSE
DISPLAY '@ OTHER STUFF'
END-IF
.
Produces the following output:
@ OTHER STUFF
A B C@ OTHER STUFF
USE ALL 30 CHARACTERS -------X@ OTHER STUFF
Note that the PERFORM VARYING statement relies on the left to right evaluation of the UNTIL clause to avoid out-of-bounds subscripting on USER-INPUT in the case where it contains only blank spaces.
Solution 2
Use OpenCOBOL 1.1 or greater.
Identification division.
Program-id. 'trimtest'.
*> Compile:
*> cobc -x -free -ffunctions-all TrimTest.cbl
*>
Data division.
Working-Storage Section.
1 myBigStr Pic X(32768) Value Spaces.
Procedure Division.
Display "Enter Something? " With no advancing.
Accept myBigStr.
Display "[" Trim(myBigStr) "]".
Goback.
The trim function also has the options; Leading or Trailing. cobc -h formore info.
Solution 3
Here's a solution if you work on OpenVMS:
01 WS-STRING-LENGTH PIC S9(04) COMP.
CALL "STR$TRIM" USING BY DESCRIPTOR user_output,
user_input,
BY REFERENCE WS-STRING-LENGTH.
Shawn J. Goff
I've grown up with computers around me my whole life. I started programming when I was a kid, and stuck with it ever since. I came across Linux around 1999 and have enjoyed working with it ever since. For a job, I get to write software that runs on embedded Linux devices. I do everything from working on low-level drivers to writing shell scripts and even web apps.
Updated on July 09, 2022Comments
-
Shawn J. Goff almost 2 years
I'm just learning COBOL; I'm writing a program that simply echos back user input. I have defined a variable as:
User-Input PIC X(30).
Later when I ACCEPT User-Input, then DISPLAY User-Input " plus some extra text", it has a bunch of spaces to fill the 30 characters. Is there a standard way (like Ruby's str.strip!) to remove the extra spaces?
-
NealB over 14 yearsNote: In the above answer I assumed that you wanted to remove trailing spaces only. If you want to remove leading spaces, a similar approach can be used starting at the front of USER-INPUT or you could try playing with the INSPECT verb with TALLYING and LEADING modifiers. Sorry, but sting management is not one of COBOL's strong points.
-
Luc M over 14 yearsWith this solution, the content of user-input won't be completely displayed if it contains a space in the middle.
-
Rick Smith about 6 years" this is pretty much the standard solution", except
LENGTH OF
is a non-standard extension. -
mckenzm over 5 years+1 for System Service call, sorry to necro comment but for z/OS with DB2 there are dirty ways using embedded SQL without table access as well.
-
Benedikt Böhm over 3 yearsGives me an error: 'Trim' is not defined Think, you are missing a "function" before "trim", so second to last line should be Display "[" function Trim(myBigStr) "]".