COBOL built-in function to trim spaces.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
BobP
Registered Member
Posts: 55
Joined: Mon Jun 17, 2013 1:33 pm
United States of America

COBOL built-in function to trim spaces.

Post by BobP »

HI All,

Is there any COBOL built-in function to trim spaces at the starting and ending of a variable.. please let me know.
Thanks,
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1900
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: COBOL built-in function to trim spaces.

Post by Robert Sample »

This question is one that you can answer for yourself by simply consulting the Enterprise COBOL Language Reference manual -- it has a section on intrinsic functions that discusses each of them in detail. Please learn to read the manuals before posting questions on this -- or any -- forum.

And the answer to your question is no, COBOL has no function to trim spaces. This is because COBOL does not have strings like languages such as C or Java. COBOL has variables. Variables are (almost always) fixed length and if there are not enough characters provided for the length of the variable, COBOL automatically fills the rest of the variable with spaces. So a trim function would be useless for COBOL since if trailing spaces were removed, they would be added right back to ensure the variable is the correct length. And since COBOL has a JUSTIFIED RIGHT clause, those spaces could be on the front or back of the variable.

The only COBOL variables that are not fixed length are variables in the LINKAGE SECTION or FILE SECTION that are defined with OCCURS DEPENDING ON. Variables defined in WORKING-STORAGE with OCCURS DEPENDING ON are defined at their maximum length so they can have trailing spaces as well.
BobP
Registered Member
Posts: 55
Joined: Mon Jun 17, 2013 1:33 pm
United States of America

Re: COBOL built-in function to trim spaces.

Post by BobP »

Robert Sample wrote: Fri Sep 28, 2018 3:55 pmAnd the answer to your question is no, COBOL has no function to trim spaces. This is because COBOL does not have strings like languages such as C or Java. COBOL has variables. Variables are (almost always) fixed length and if there are not enough characters provided for the length of the variable, COBOL automatically fills the rest of the variable with spaces. So a trim function would be useless for COBOL since if trailing spaces were removed, they would be added right back to ensure the variable is the correct length. And since COBOL has a JUSTIFIED RIGHT clause, those spaces could be on the front or back of the variable.

The only COBOL variables that are not fixed length are variables in the LINKAGE SECTION or FILE SECTION that are defined with OCCURS DEPENDING ON. Variables defined in WORKING-STORAGE with OCCURS DEPENDING ON are defined at their maximum length so they can have trailing spaces as well.
Thanks for the detailed explanation. I did search for this in the manual but was not sure if I have missed something in that search. Wanted to make sure that I have not missed something.
Thanks,
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”