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 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 is used to count the string characters.
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.
Let’s see an example for Tallying option:
Replacing option replaces the string characters.
Here, the input-string parameter is the string whose characters are to be replaced from char1 to char2.
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.
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.
Let’s see an example for String verb:
The UNSTRING verb breaks one string into several sub-strings. The ‘ DELIMITED BY ‘ clause is mandatory in the unstring verb.
Let’s see an example for Unstring verb: