If we assume a "string" to be a binary-zero-delimited piecs of data (the binary zero usually represented as /n) then COBOL does not have that. COBOL has fixed-lenth fields, which will be "padded" to the right with spaces if the data is short of the field-length and truncated to the right if the data is longer than the field.
Enterprise COBOL does have string literals - consult the manual.
To find the length of the data in a field, you have to count the trailing spaces.
There are two main ways to do this: use INSPECT with TALLYING for LEADING SPACES on a FUNCTION REVERS field (INSPECT on the Mainfframe does not have TRAILING, altough some other compilers have it as a non-Standard addition)); code it out yourself.
If coding it out yourself, there are two main ways: user reference modification; use REDEFINES and a subscript/index.
I prefer the subscripting/indexing: check the field for SPACES first (makes termination condition easy); loop backwards until non-space (note that look will not process if first character in the loop (last byte of field) is non-space).
Also, I feel that in cobol it should not be called string lenght but then this is what i heard other saying it, is it correct? Please help me learn.
This terminology is not correct. A string in most languages can be variable length, while COBOL variables are generally fixed length. The only exceptions are for LINKAGE SECTION and FILE SECTION variables defined using OCCURS DEPENDING ON where the length of the data will depend on a variable. WORKING-STORAGE SECTION variables defined as OCCURS DEPENDING ON are generated with the maximum length possible, so they too are fixed length.
You can have a variable-length field (the amount of storage allocated is fixed) using OCCURS DEPENDING ON. It is still not a "string", because a string would be terminated, instead of this the variable-length field has a length.
01 WS-TEXT PIC X(40) VALUE 'THIS IS THE VARIABLE VALUE'.
INSPECT FUNCTION REVERSE(WS-TEXT) TALLYING L FOR LEADING SPACES
COMPUTE LEN = LENGTH OF WS-TEXT - LEN
William Collins wrote:You can have a variable-length field (the amount of storage allocated is fixed) using OCCURS DEPENDING ON. It is still not a "string", because a string would be terminated, instead of this the variable-length field has a length.
Sorry hit the submit button too fast. William sorry I did not understand your explnation, can you please share some example with me?
Thanks William. I shall not be able to try your example right away as I don't have a mainframe to test it but I understand the logic somewhat moving 10, 17 and zero to lengeth of the field is not clear to me. What is that doing?
If you'd like to have your own COBOL compiler to try things out when you don't have access to a Mainframe, see here, https://sourceforge.net/p/open-cobol/di ... rce=navbar to download one. It is not Enterprise COBOL but it will allow you to try things out.
The examples are as Pandora-Box describes. As output from the DISPLAY you would see, bounded by > and <, date of 10, 17 and zero bytes.