Page 1 of 1

COBOL built-in function to trim spaces.

Posted: Fri Sep 28, 2018 9:38 am
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.

Re: COBOL built-in function to trim spaces.

Posted: Fri Sep 28, 2018 3:55 pm
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.

Re: COBOL built-in function to trim spaces.

Posted: Mon Oct 01, 2018 8:24 am
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.