STRING AND UNSTRING IN COBOL

STRING AND UNSTRING with examples: TALLYING and COUNT options

String is used to combine two or more strings/variables in to a single string.

Examples:

If you display the ‘name-in’ it shows like ‘ Mahender Reddy G ‘ i.e: it shows the full length of each variable INCLUDING THE SPACES.

So this can be avoided by using the STRING function.

Now NAME-OUT would be ‘mahenderreddyg ‘.

If we want to further modify the output , it can also be done using STRING

Now NAME-OUT would be ‘Mahender Reddy G ‘.

UNSTRING verb is used to unstring/divide the source string into different sub-strings.

If we take the same above example-

NAME-OUT is ‘Mahender Reddy G ‘ and it needs to be divided into first name, last name and initial; it can be done using unstring verb.

Here we have used the delimiter SPACE, but we can use any other delimiter as well.

for ex: if we have a string with name list seperated by commas and we want to exatract each name into an array.

name-in-string is ‘mahender,ramu, robert,phil,Chris’, these can be separated as below

More UNSTRING Examples:

Result:

Example 2:
05 WW-U4-INPUT PIC X(12) VALUE ‘AA BB CC ‘.
* there are 2 spaces in between BB & CC

Result:

For the same ABOVE INPUT VALUE, if we change the Unstring value with ‘DELIMITED BY ALL SPACES Instead of ‘delimited by SPACE.

If More than one delimiter is present in between two strings, then ALL should be used.

Result:

Example 3:

Result:

Note – Count takes Spaces also into consideration.

Example 4: Multiple delimiters by Using OR in Unstring

 

Example 5: Tallying Option

It gives the count of number of receiving fields. If there are 4 fields in INTO clause then the count would be 4.

 

In this case WW-TL-1 contains a value of 4.
Note: TALLYING SHOULD BE CODED for the last receiving field.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami