COBOL String Handling

A string can have multiple operations involved in it:

Example:
  • Concatenation is Combining multiple strings into one. We can do this using ‘STRING‘ operation.
  • To split a string into various other substrings. We do this using ‘UNSTRING‘ operation.
  • To find out the pattern in a particular string using ‘INSPECT
  • To replace a particular character or a group of alphabets with other character or group of alphabets respectively using ‘INSPECT

STRING

For combining multiple strings into one destination string we use  String.

Uses of** ‘DELIMITED BY‘ Clause is to decide the position from where the string needs to be formatted

Syntax

STRING Identifier1/literal1 DELIMITED BY identifier22/literal22/SIZE
            Identifier2/literal2 DELIMITED BY identifier33/literal33/SIZE
            INTO identifier3
            WITH POINTER  pointer-id
            ON OVERFLOW {imperative statement1}
            NOT ON OVERFLOW {imperative statement2}
END-STRING.

Let us understand all the important terms used for STRING Operation.

DELIMITED BY​

‘DELIMITED BY’ is an Optional phrase which we use to determine the limits or boundaries of the string operation.

Let us understand the important ‘DELIMITED BY’ phrases-

DELIMITED BY SIZE

To limit the string based on the size, we use ‘DELIMITED BY SIZE’. While performing String operations, if a particular string has to be concatenated based on size, then ‘DELIMITED BY SIZE’ we use. In this case, the actual size of the source string is considered as a delimiter.

DELIMITED BY SPACE

To limit the string based on the initial space, we use ‘DELIMITED BY SPACE’. While performing String operations, if a particular string has to be concatenated based on initial space, then DELIMITED BY SPACE is used. In this case, the initial space of the source string is considered as a delimiter.

DELIMITED BY other-delimiter

To limit the string based on any other delimiter(apart from SIZE and SPACE), we use that delimiter along with ‘DELIMITED BY’ clause. While performing String operations, if a particular string has to be concatenated based on another delimiter like dot(.), Hyphens(-), Equal(=) etc, then ‘DELIMITED BY other-delimiters’ is used.

WITH POINTER​

WITH POINTER‘ is an optional Phrase we use this to point the starting position of the destination string item.

SYNTAX:

WITH POINTER N
Here, N – Numeric, 

Length of the string item > N >= 0

ON OVERFLOW​

  • it is an optional Phrase in String handling.
  • We set this condition is set when the size of the destination string is less than the size of the string which is form after the concatenation.
  • We can execute an imperative statement once ‘ON OVERFLOW’ condition meets in a String operation.

NOT ON OVERFLOW​

  • It is an optional Phrase.
  • This condition is set when the size of the destination string is greater than or equal to the size of the string which is formed after the concatenation.
  • It is a case of successful concatenation. We can execute an imperative statement once this condition is met.

Sample COBOL Program to show STRING operation​

TutorialBrain-STRING operation used in String handling in COBOL
The TutorialBrain-STRING operation used in String handling in COBOL

Output

TutorialBrain-Output of the STRING operation used in String handling in COBOL
The Output of simple IF-ELSE conditional processing in COBOL

UNSTRING

COBOL UNSTRING is the opposite of STRING. It splits a String into smaller sub strings.

** ‘DELIMITED BY‘ Clause we use to decide the position from where the string needs to be formatted

Syntax​

UNSTRING Identifier/literal DELIMITED BY [ALL] identifier22/literal22/SIZE
   INTO identifier1/literal1 [DELIMITER IN] Hold-identifier1 COUNT IN identifier123
   identifier2/literal2 [DELIMITER IN] Hold-delimiter-1 COUNT IN identifier456
   identifier3/literal3 [DELIMITER IN] Hold-identifier3 COUNT IN identifier789
   …..
   [WITH POINTER identifier-for-pointer]   [ TALLYING IN tally-identifier]   [ON OVERFLOW {imperative statement1}]   [NOT ON OVERFLOW {imperative statement2}]END-UNSTRING.

Let us understand all the important terms used for UNSTRING Operation.

DELIMITER IN Hold-delimiter-1​

  • ‘DELIMITER IN’ informs the system about the field where the delimiter has to be received.
  • If we are not using ‘DELIMITER BY’ clause, then we should also not use ‘DELIMITER IN’ clause as well.
  • a hold-delimiter-1 field contains the integer value

COUNT IN identifier123​

  • ‘COUNT IN’ this phrase follows a count field which contains the final count of each character in that particular receiving field.
  • If we are not using ‘DELIMITER BY’ clause, then we should not use ‘DELIMITER IN’ clause as well.

WITH POINTER identifier-for-pointer​

We use ‘WITH POINTER’ phrase to save the final count after reading each character of the sending fields. Each character read adds 1 to the count.

TALLYING IN tally-identifier​

TALLYING phrase we use with an identifier. In this case, tally-identifier contains the final value which is equal to the initial value plus the number of receiving string fields.

Note: The variable tally-identifier must be initialized before it is used in UNSTRING operation.

ON OVERFLOW {imperative statement1}​

ON OVERFLOW‘ condition is set:

1) When the pointer value is less than 1.
2) The pointer value exceeds a value equal to the length of the sending field.
3) All data receiving fields have been used but the sending field still contains character positions which are not read.

NOT ON OVERFLOW {imperative statement2}

NOT ON OVERFLOW‘ condition is the case of successful splitting of UNSTRING operation.

Sample COBOL Program to show UNSTRING operation​

TutorialBrain-UNSTRING operation used in String handling in COBOL
The TutorialBrain-UNSTRING operation used in String handling in COBOL

Output

TutorialBrain-Output of UNSTRING operation used in String handling in COBOL
The TutorialBrain-Output of UNSTRING operation used in String handling in COBOL

We can search for a specific pattern in String using INSPECT. An important operation involving the replacement of a character or a group of characters with other character or group of characters is also performed using ‘INSPECT’ with ‘REPLACING’ Option. Let us understand more about INSPECT.

INSPECT​

COBOL INSPECT is the used to analyze a string to either do any of these of the following:

1. TALLYING​

TALLYING is used to count the appearance of an alphabet or character or a sub-string in a String, we use TALLYING options for this purpose

Syntax​

INSPECT source-string
TALLYING tallying-counter FOR ALL CHARACTERS [BEFORE INITIAL ‘XYZ’]  [AFTER INITIAL ‘ABC’]

Sample Program to show the TALLYING operation in String handling in COBOL​

TutorialBrain-TALLYING operation used in UNString in COBOL
The TutorialBrain-TALLYING operation used in UNString in COBOL

Output

The TutorialBrain-Output of TALLYING operation used in UNString in COBOL

2. REPLACING​

To replace an alphabet or character or a sub-string in a String, we use ‘REPLACING’           options for this purpose.

Syntax​

INSPECT input-string REPLACING [ALL] sub-str-1 BY sub-str-1 [BEFORE INITIAL ‘XYZ’]  [AFTER INITIAL ‘ABC’]

Sample Program to show the REPLACING operation in String handling in COBOL

TutorialBrain-REPLACING operation used in UNSTRING in COBOL
The TutorialBrain-REPLACING operation used in UNSTRING in COBOL

Output

TutorialBrain-Output of REPLACING operation used in UNSTRING in COBOL

Tutorials for all brains!