Cobol and Mainframe

COBOL Substring

COBOL substring is a powerful feature of the COBOL programming language that allows the programmers to easily extract specific parts of a string of data. This substring can be used to perform various operations such as string manipulation, pattern matching, calculations, and data analysis. COBOL substring can be used to make the programs more efficient, flexible, and maintainable. COBOL substring is a built-in function of the COBOL language, and is used to extract a substring from a given string.

What Does the SUBSTR Function Do in COBOL?

COBOL offers the SUBSTR function to extract a portion of a string by providing the source string, the starting position, and the length of the desired substring. This functionality allows for efficient handling of string data in COBOL programming.

SUBSTR function extracts a portion of a string starting from the nth character and with a specified length by the user. If the extracted substring is of a lesser length than the one that is specified, it is padded with the desired character.

If the value of n exceeds the length of the input string, the result that is obtained from the SUBSTR function consists of pad characters.

If you do not provide a value for the length, the function returns the remainder of the string from the starting position. By default, the pad character that is used is a blank.

Syntax:

SUBSTR(source-str, start-position, substr-len)

 

The source-string is the string from which you want to extract the substring. The “start-position” is the starting index of the substring in the “source-str”. And “substr-len” is the length of the substring that you want to extract.

Programming Example 1:

IDENTIFICATION DIVISION.
PROGRAM-ID. SUBSTRING-EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  SOURCE-STRING   PIC X(20) VALUE 'HELLO WORLD'.
01  FINAL-STRING   PIC X(5).
PROCEDURE DIVISION.
MAIN-LOGIC.
    STRING SOURCE-STRING DELIMITED BY ' ' INTO FINAL-STRING.
    DISPLAY FINAL STRING: ' FINAL-STRING.
STOP RUN.

 

Output:

papan@ubuntu:~/Desktop$ cobc -xjF 1.cbl
FINAL STRING: HELLO

 

Explanation:

In this program, we define a source string of length 20 and a FINAL string of length 5. We initialize the source string with the “HELLO WORLD” value. We then use the STRING function to extract the first word, “HELLO”, from the source string and store it in the FINAL string. We then display the FINAL string using the DISPLAY statement.

Programming Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. SUBSTRINGS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  SOURCE-STRING   PIC X(20) VALUE 'ABCDE'.
01  SUBSTRING-LEN   PIC 9(2) VALUE 1.
01  START-INDEX     PIC 9(2) VALUE 1.
01  END-INDEX       PIC 9(2).
01  SUB-STRING      PIC X(20).
PROCEDURE DIVISION.
MAIN-LOGIC.
    PERFORM VARYING SUBSTRING-LEN FROM 1 BY 1
        UNTIL SUBSTRING-LEN > 20
        PERFORM VARYING START-INDEX FROM 1 BY 1
            UNTIL START-INDEX + SUBSTRING-LEN - 1 > 20
            COMPUTE END-INDEX = START-INDEX + SUBSTRING-LEN - 1
            STRING SOURCE-STRING(START-INDEX: SUBSTRING-LEN)
                   DELIMITED BY SIZE INTO SUB-STRING
            DISPLAY 'SUBSTRING: ' SUB-STRING
        END-PERFORM
    END-PERFORM
STOP RUN.

 

Output:

papan@ubuntu:~/Desktop/cbl$ cobc -xjF 2.cbl
SUBSTRING: ABCDE              
SUBSTRING: BCDE                
SUBSTRING: CDE                
SUBSTRING: DE                  
SUBSTRING: E

 

Explanation:

This program generates all possible substrings of a given source string using two nested loops to iterate over all possible substring lengths and starting positions. It stores the source string and substring length in SOURCE-STRING and SUBSTRING-LEN, respectively. Then, it uses the START-INDEX and END-INDEX to keep track of the current starting and ending positions. The program then extracts the current substring using the STRING statement and the substring function, and displays it using the DISPLAY statement.

Programming Example 3:

IDENTIFICATION DIVISION.
PROGRAM-ID. INITIALS-PROGRAM.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-FULL-NAME PIC X(30) VALUE 'Subash Chandra Bose'.
01 WS-FIRST-NAME PIC X(10).
01 WS-MIDDLE-NAME PIC X(10).
01 WS-SURNAME PIC X(10).
01 WS-DOT PIC X VALUE '.'.

PROCEDURE DIVISION.
MAIN-LOGIC.
     DISPLAY ‘INPUT NAME:’ WS-FULL-NAME.



    COMPUTE WS-FIRST-NAME = FUNCTION SUBSTRING(WS-FULL-NAME, 1, INDEX(WS-FULL-NAME, ' ') - 1).

    COMPUTE WS-MIDDLE-NAME = FUNCTION SUBSTRING(WS-FULL-NAME, INDEX(WS-FULL-NAME, ' ') + 1, INDEX(WS-FULL-NAME, ' ', INDEX(WS-FULL-NAME, ' ') + 1) - INDEX(WS-FULL-NAME, ' ') - 1).

    COMPUTE WS-SURNAME = FUNCTION SUBSTRING(WS-FULL-NAME, INDEX(WS-FULL-NAME, ' ', LAST-INDEX(WS-FULL-NAME, ' ')) + 1).

    DISPLAY ‘OUTPUT:’ WS-FIRST-NAME(1:1) WS-DOT WS-MIDDLE-NAME(1:1) WS-DOT WS-SURNAME.
STOP RUN.

 

Output:

papan@ubuntu:~/Desktop/cbl$ cobc -xjF 3.cbl
INPUT NAME: Subash Chandra Bose
OUTPUT: S.C. Bose

 

Explanation:

In this program, the WS-FULL-NAME variable holds the full name of the person. The WS-FIRST-NAME, WS-MIDDLE-NAME, and WS-SURNAME variables hold the first name, middle name, and surname of the person, respectively. The INDEX and LAST-INDEX functions are used to find the positions of the first and last spaces in the WS-FULL-NAME variable. Then, we print the initials of the first and middle name, followed by the surname, separated by a period.

Conclusion

We explored the SUBSTR function in COBOL and provided some examples on how to use it in practical applications. The SUBSTR function is a useful feature in business applications where the data often needs to be manipulated or transformed. With the SUBSTR function, the programmers can easily extract the substrings from larger strings, and use them for further processing. Hopefully, this article provided a useful introduction to the SUBSTR function in COBOL, and properly demonstrated some of its practical examples.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com