COBOL String Handling Techniques

When working with COBOL, mastering string manipulation is crucial for effective data processing and reporting. In this article, we will delve into various techniques for handling strings in COBOL, specifically focusing on concatenation and substring extraction.

Understanding STRING Data Type

Before diving into string manipulation techniques, it's essential to understand how strings are represented in COBOL. In COBOL, strings are typically represented using the PIC or Picture clause, where characters can be defined using the following formats:

  • A: Alphabetic characters (A-Z, a-z)
  • N: Numeric characters (0-9)
  • X: Alphanumeric characters (A-Z, a-z, 0-9, and special characters)

For example:

01  customer-name     PIC X(30).
01  order-number      PIC 9(10).

Concatenation of Strings

One of the most common operations performed on strings is concatenation, which combines two or more strings into a single string. COBOL provides a useful intrinsic function called CONCATENATE, as well as a straightforward operator called STRING.

Using the STRING Statement

The STRING statement is used to concatenate strings efficiently:

01  first-name        PIC X(15).
01  last-name         PIC X(15).
01  full-name         PIC X(31).

MOVE "John" TO first-name.
MOVE "Doe"  TO last-name.

STRING first-name DELIMITED BY SPACE
       last-name DELIMITED BY SIZE
       INTO full-name.

In the snippet above, DELIMITED BY SPACE tells the compiler to treat the first name as a delimited string, adding a space between the first and last name. The full name will be stored in full-name.

Using the CONCATENATE Function

COBOL also allows concatenation through the CONCATENATE function:

01  greeting         PIC X(50).
01  say-hello        PIC X(20) VALUE "Hello, ".
01  user-name        PIC X(30).

MOVE "Alice" TO user-name.

MOVE FUNCTION CONCATENATE(say-hello, user-name) TO greeting.

Here, the FUNCTION CONCATENATE combines two strings into one, storing the result in greeting. Using functions can be beneficial for more complex concatenation scenarios.

Substring Extraction

Extracting substrings is another fundamental operation when dealing with strings. COBOL provides the SUBSTRING function that allows you to extract part of a string based on its position.

Using the SUBSTRING Function

The syntax for the SUBSTRING function is straightforward:

01  source-string    PIC X(50) VALUE "COBOL is interesting!".
01  sub-section      PIC X(20).

MOVE FUNCTION SUBSTRING(source-string, 1, 6) TO sub-section.

In the example above, FUNCTION SUBSTRING(source-string, 1, 6) extracts the first six characters from source-string, resulting in sub-section containing "COBOL".

Specifying Range Marks

When using the SUBSTRING function, you can specify the position and length:

01  new-string       PIC X(50).

MOVE FUNCTION SUBSTRING(source-string, 8, 2) TO new-string.

This extracts two characters starting from the eighth position, so new-string will contain "is".

Combining Concatenation and Substring Techniques

Often, you will need to use concatenation and substring extraction together. Let's look at a scenario where we extract a portion of a string and concatenate it with another string:

01  item-code        PIC X(10) VALUE "ITEM123456".
01  short-item-code  PIC X(5).
01  display-string    PIC X(20).

MOVE FUNCTION SUBSTRING(item-code, 6, 5) TO short-item-code.
STRING "Short Code: " DELIMITED BY SIZE
       short-item-code DELIMITED BY SIZE
       INTO display-string.

In this snippet, we extract the last five characters from item-code, yielding "3456", then combine it with "Short Code: " to form display-string, which would then contain "Short Code: 3456".

Handling Spaces and Trimming

When concatenating strings, culling unwanted spaces is often necessary. COBOL provides the TRIM function to achieve this:

01  padded-string    PIC X(30) VALUE "   padded text    ".
01  trimmed-string    PIC X(30).

MOVE FUNCTION TRIM(padded-string) TO trimmed-string.

The TRIM function removes leading and trailing spaces from padded-string, leaving trimmed-string as "padded text".

Error Handling in String Operations

When manipulating strings, it is crucial to incorporate error handling to manage unexpected results, especially when dealing with dynamic data. One approach is to validate the lengths before performing operations:

IF LENGTH(source-string) >= 8
   MOVE FUNCTION SUBSTRING(source-string, 1, 8) TO new-string
ELSE
   MOVE "Error: String too short!" TO new-string.

The above checks whether source-string has at least eight characters before attempting to extract the substring.

Conclusion

In COBOL, string handling techniques such as concatenation and substring extraction are essential tools for developers. With methods like STRING, CONCATENATE, and the SUBSTRING function, you can manipulate strings effectively. Remember to handle spaces and potential errors when dealing with dynamic strings to ensure robust and efficient COBOL applications.

By applying these techniques, you'll be able to work with strings in COBOL like a pro, enhancing your programming skills and enabling the creation of more sophisticated software solutions. Happy coding!