COBOL – String Handling
String handling statements are used in COBOL to perform operations of string manipulation. COBOL provides three types of statements for string handling:
- INSPECT
- STRING
- UNSTRING
INSPECT
Inspect verb replaces or counts a character or a group of characters in a string. On numeric, alphabetic, or alphanumeric values, we can perform string operations. Inspect operations are work from left to right.
The Inspect provides two options are as follows:
Tallying?
Tallying is used to count the string characters.
Syntax:
Here, input-string and output string are parameters. Input-string is the string whose characters are to be counted. And output-string is data item to hold the count of characters.
Example:
Let’s see an example for Tallying option:
Output:
Replacing
Replacing option replaces the string characters.
Syntax:
Here, the input-string parameter is the string whose characters are to be replaced from char1 to char2.
Example:
Output:
STRING
The string verb is used to concatenate the partial or full contents of two or more string or literal into one single data item. In the String verb, the ‘DELIMITED BY’ clause is compulsory.
Syntax:
Here, used parameters details are:
- ws-string1 and ws-string2: These are input strings to be concatenated.
- ws-string: This is an output string.
- ws-count: This is used to count the length of the new concatenated string.
- Delimited: used to specify the end of the string.
- Pointer and Overflow: These are optional.
Example:
Let’s see an example for String verb:
Output:
UNSTRING
The UNSTRING verb breaks one string into several sub-strings. The ‘ DELIMITED BY ‘ clause is mandatory in the unstring verb.
Syntax:
Example:
Let’s see an example for Unstring verb:
Output: