COBOL provides powerful string manipulation capabilities through several specialized statements and functions. These operations allow you to concatenate, split, search, replace, and analyze string data efficiently.
COBOL offers four primary string manipulation tools:
Each operation serves specific purposes and can be combined for complex string processing.
Operation | Use Case | Example |
---|---|---|
STRING | Building full names, addresses, messages | First + Last name |
UNSTRING | Parsing CSV data, splitting delimited fields | Comma-separated values |
INSPECT | Data cleaning, character replacement | Remove spaces, convert case |
Functions | Length checking, trimming, validation | Get string length, trim spaces |
The STRING statement concatenates multiple source strings into a single target string. It provides flexible control over how strings are combined and handles overflow conditions.
1234567STRING {source-string-1} DELIMITED BY {delimiter-1} {source-string-2} DELIMITED BY {delimiter-2} ... INTO target-string [ON OVERFLOW imperative-statement] [NOT ON OVERFLOW imperative-statement] [END-STRING]
The STRING statement combines multiple source strings into the target string, using DELIMITED BY to control when each source string ends.
The DELIMITED BY clause specifies when to stop processing each source string:
Option | Description | Example |
---|---|---|
SIZE | Use entire field (including spaces) | DELIMITED BY SIZE |
SPACE | Stop at first space | DELIMITED BY SPACE |
SPACES | Stop at first space | DELIMITED BY SPACES |
Character | Stop at specified character | DELIMITED BY "," |
Identifier | Stop at value in field | DELIMITED BY WS-DELIMITER |
12345678910111213141516171819202122232425* Simple concatenation STRING FIRST-NAME DELIMITED BY SPACE, LAST-NAME DELIMITED BY SIZE INTO FULL-NAME. * With literal strings STRING "Hello, " DELIMITED BY SIZE, CUSTOMER-NAME DELIMITED BY SPACE, "!" DELIMITED BY SIZE INTO GREETING-MESSAGE. * Using different delimiters STRING FIELD1 DELIMITED BY ",", FIELD2 DELIMITED BY SIZE INTO COMBINED-FIELD. * With overflow handling STRING FIELD1 DELIMITED BY SIZE, FIELD2 DELIMITED BY SIZE INTO TARGET-FIELD ON OVERFLOW PERFORM ERROR-HANDLING NOT ON OVERFLOW DISPLAY "String operation successful" END-STRING.
These examples show various ways to use the STRING statement for different concatenation needs.
12345678910111213141516171819202122232425262728* Building addresses STRING STREET-ADDRESS DELIMITED BY SPACE, ", " DELIMITED BY SIZE, CITY DELIMITED BY SPACE, ", " DELIMITED BY SIZE, STATE DELIMITED BY SPACE, " " DELIMITED BY SIZE, ZIP-CODE DELIMITED BY SIZE INTO FULL-ADDRESS. * Creating formatted messages STRING "Customer ID: " DELIMITED BY SIZE, CUSTOMER-ID DELIMITED BY SIZE, " Name: " DELIMITED BY SIZE, CUSTOMER-NAME DELIMITED BY SPACE, " Amount: $" DELIMITED BY SIZE, AMOUNT DELIMITED BY SIZE INTO LOG-MESSAGE. * With conditional concatenation IF CUSTOMER-TYPE = "VIP" STRING "VIP " DELIMITED BY SIZE, CUSTOMER-NAME DELIMITED BY SPACE INTO DISPLAY-NAME ELSE STRING CUSTOMER-NAME DELIMITED BY SPACE INTO DISPLAY-NAME END-IF.
Advanced examples demonstrate complex string building scenarios with multiple fields and conditional logic.
The UNSTRING statement parses a source string and splits it into multiple target fields based on delimiters. It's the opposite of STRING and is useful for parsing delimited data.
12345678910UNSTRING source-string DELIMITED BY {delimiter-1} [OR {delimiter-2}] ... INTO target-field-1 [DELIMITER IN delimiter-field-1] target-field-2 [DELIMITER IN delimiter-field-2] ... [WITH POINTER pointer-field] [TALLYING IN tally-field] [ON OVERFLOW imperative-statement] [NOT ON OVERFLOW imperative-statement] [END-UNSTRING]
The UNSTRING statement splits the source string into multiple target fields, using delimiters to determine where to split.
1234567891011121314151617181920212223* Simple splitting by comma UNSTRING CSV-LINE DELIMITED BY "," INTO FIELD1, FIELD2, FIELD3. * Splitting by multiple delimiters UNSTRING INPUT-LINE DELIMITED BY "," OR ";" OR SPACE INTO NAME-FIELD, ADDRESS-FIELD, PHONE-FIELD. * With delimiter tracking UNSTRING PARSED-LINE DELIMITED BY "," INTO FIELD1 DELIMITER IN DELIM1, FIELD2 DELIMITER IN DELIM2, FIELD3 DELIMITER IN DELIM3. * With pointer and tally UNSTRING SOURCE-STRING DELIMITED BY SPACE INTO TARGET1, TARGET2, TARGET3 WITH POINTER WS-POSITION TALLYING IN WS-FIELD-COUNT.
These examples demonstrate different ways to use UNSTRING for parsing delimited data.
1234567891011121314151617181920212223242526272829303132* Parsing CSV data UNSTRING CSV-RECORD DELIMITED BY "," INTO CUSTOMER-ID DELIMITER IN WS-DELIM1, CUSTOMER-NAME DELIMITER IN WS-DELIM2, CUSTOMER-ADDRESS DELIMITER IN WS-DELIM3, CUSTOMER-PHONE DELIMITER IN WS-DELIM4 WITH POINTER WS-POSITION TALLYING IN WS-FIELD-COUNT ON OVERFLOW PERFORM HANDLE-OVERFLOW NOT ON OVERFLOW DISPLAY "Parsed " WS-FIELD-COUNT " fields" END-UNSTRING. * Parsing fixed-format with variable delimiters UNSTRING LOG-ENTRY DELIMITED BY "|" OR ":" OR SPACE INTO TIMESTAMP DELIMITER IN WS-TIME-DELIM, LOG-LEVEL DELIMITER IN WS-LEVEL-DELIM, MESSAGE DELIMITER IN WS-MSG-DELIM. * Parsing with conditional logic IF RECORD-TYPE = "CUSTOMER" UNSTRING CUSTOMER-LINE DELIMITED BY "," INTO CUST-ID, CUST-NAME, CUST-ADDRESS ELSE UNSTRING PRODUCT-LINE DELIMITED BY "|" INTO PROD-ID, PROD-NAME, PROD-PRICE END-IF.
Advanced examples show complex parsing scenarios with error handling and conditional processing.
Clause | Purpose | Example |
---|---|---|
DELIMITED BY | Specify delimiter(s) for splitting | DELIMITED BY "," |
DELIMITER IN | Store the delimiter used | DELIMITER IN WS-DELIM |
WITH POINTER | Track position in source string | WITH POINTER WS-POS |
TALLYING IN | Count fields processed | TALLYING IN WS-COUNT |
ON OVERFLOW | Handle overflow conditions | ON OVERFLOW PERFORM ERROR |
The INSPECT statement analyzes and modifies strings by counting, replacing, or converting characters. It's a powerful tool for string manipulation and data cleaning.
12345INSPECT target-string TALLYING count-field FOR {ALL | LEADING | FIRST} {characters | identifier} [REPLACING {ALL | LEADING | FIRST} {characters | identifier} BY {characters | identifier}] [CONVERTING {characters | identifier} TO {characters | identifier}] [END-INSPECT]
INSPECT can perform counting, replacing, and converting operations on strings.
12345678910111213141516171819* Count all occurrences of a character INSPECT TEXT-FIELD TALLYING WS-COUNT FOR ALL "A". * Count leading spaces INSPECT NAME-FIELD TALLYING WS-SPACE-COUNT FOR LEADING SPACE. * Count first occurrence INSPECT ADDRESS-FIELD TALLYING WS-COMMA-COUNT FOR FIRST ",". * Count multiple characters INSPECT DATA-FIELD TALLYING WS-VOWEL-COUNT FOR ALL "A" "E" "I" "O" "U". * Count with identifier INSPECT INPUT-FIELD TALLYING WS-DIGIT-COUNT FOR ALL WS-DIGITS.
TALLYING counts occurrences of specified characters in the target string.
123456789101112131415161718192021* Replace all occurrences INSPECT TEXT-FIELD REPLACING ALL "A" BY "X". * Replace leading characters INSPECT NAME-FIELD REPLACING LEADING SPACE BY "0". * Replace first occurrence INSPECT ADDRESS-FIELD REPLACING FIRST "," BY " - ". * Replace multiple characters INSPECT DATA-FIELD REPLACING ALL "A" BY "X" ALL "B" BY "Y" ALL "C" BY "Z". * Replace with identifier INSPECT INPUT-FIELD REPLACING ALL WS-OLD-CHAR BY WS-NEW-CHAR.
REPLACING substitutes characters in the target string based on specified rules.
1234567891011121314151617* Convert case (lower to upper) INSPECT TEXT-FIELD CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ". * Convert case (upper to lower) INSPECT NAME-FIELD CONVERTING "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO "abcdefghijklmnopqrstuvwxyz". * Convert specific characters INSPECT DATA-FIELD CONVERTING "0123456789" TO "ABCDEFGHIJ". * Convert with identifiers INSPECT INPUT-FIELD CONVERTING WS-SOURCE-CHARS TO WS-TARGET-CHARS.
CONVERTING transforms characters from one set to another, useful for case conversion and character mapping.
1234567891011121314151617181920* Count and replace in one operation INSPECT TEXT-FIELD TALLYING WS-SPACE-COUNT FOR ALL SPACE REPLACING ALL SPACE BY "-". * Multiple operations INSPECT DATA-FIELD TALLYING WS-COMMA-COUNT FOR ALL "," REPLACING ALL "," BY ";" CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ". * Complex data cleaning INSPECT INPUT-FIELD TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" ALL " " BY " " ALL "A" BY "X" CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
INSPECT can combine multiple operations for complex string manipulation.
COBOL provides several built-in functions for string analysis and manipulation. These functions return values that can be used in expressions and calculations.
Function | Purpose | Example | Result |
---|---|---|---|
FUNCTION LENGTH | Get string length (no trailing spaces) | FUNCTION LENGTH("ABC ") | 3 |
FUNCTION TRIM | Remove leading/trailing spaces | FUNCTION TRIM(" ABC ") | "ABC" |
FUNCTION UPPER-CASE | Convert to uppercase | FUNCTION UPPER-CASE("abc") | "ABC" |
FUNCTION LOWER-CASE | Convert to lowercase | FUNCTION LOWER-CASE("ABC") | "abc" |
FUNCTION REVERSE | Reverse string characters | FUNCTION REVERSE("ABC") | "CBA" |
1234567891011121314151617181920212223* Get string length COMPUTE WS-LENGTH = FUNCTION LENGTH(WS-STRING). * Trim spaces MOVE FUNCTION TRIM(WS-INPUT) TO WS-CLEAN-STRING. * Convert case MOVE FUNCTION UPPER-CASE(WS-NAME) TO WS-UPPER-NAME. MOVE FUNCTION LOWER-CASE(WS-CODE) TO WS-LOWER-CODE. * Reverse string MOVE FUNCTION REVERSE(WS-PALINDROME) TO WS-REVERSED. * Combined operations COMPUTE WS-CLEAN-LENGTH = FUNCTION LENGTH(FUNCTION TRIM(WS-STRING)). * In conditional statements IF FUNCTION LENGTH(WS-NAME) > 10 DISPLAY "Name too long" END-IF. * In calculations COMPUTE WS-POSITION = FUNCTION LENGTH(WS-PREFIX) + 1.
String functions can be used in expressions, assignments, and conditional statements.
123456789101112131415161718192021222324* Data validation with functions IF FUNCTION LENGTH(FUNCTION TRIM(WS-CUSTOMER-NAME)) = 0 DISPLAY "Customer name is required" MOVE "ERROR" TO WS-STATUS END-IF. * Case-insensitive comparison IF FUNCTION UPPER-CASE(WS-INPUT) = FUNCTION UPPER-CASE(WS-EXPECTED) MOVE "MATCH" TO WS-RESULT END-IF. * String formatting STRING FUNCTION TRIM(WS-FIRST-NAME) DELIMITED BY SIZE, " " DELIMITED BY SIZE, FUNCTION TRIM(WS-LAST-NAME) DELIMITED BY SIZE INTO WS-FULL-NAME. * Palindrome checking IF WS-STRING = FUNCTION REVERSE(WS-STRING) DISPLAY "String is a palindrome" END-IF. * Dynamic field sizing COMPUTE WS-FIELD-SIZE = FUNCTION LENGTH(WS-DATA) + 5.
Advanced examples show how functions can be combined for complex string processing.
These examples demonstrate real-world applications of COBOL string operations for common business scenarios.
123456789101112131415161718192021222324252627282930313233343536373839* Process customer data with string operations PROCESS-CUSTOMER-DATA. * Clean and format customer name INSPECT WS-CUSTOMER-NAME TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" ALL " " BY " " END-INSPECT * Convert name to proper case INSPECT WS-CUSTOMER-NAME CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ" END-INSPECT * Build full address STRING WS-STREET-ADDRESS DELIMITED BY SPACE, ", " DELIMITED BY SIZE, WS-CITY DELIMITED BY SPACE, ", " DELIMITED BY SIZE, WS-STATE DELIMITED BY SIZE, " " DELIMITED BY SIZE, WS-ZIP-CODE DELIMITED BY SIZE INTO WS-FULL-ADDRESS ON OVERFLOW PERFORM HANDLE-ADDRESS-OVERFLOW NOT ON OVERFLOW DISPLAY "Address formatted successfully" END-STRING * Validate email format IF FUNCTION LENGTH(FUNCTION TRIM(WS-EMAIL)) > 0 INSPECT WS-EMAIL TALLYING WS-AT-COUNT FOR ALL "@" END-INSPECT IF WS-AT-COUNT NOT = 1 DISPLAY "Invalid email format" END-IF END-IF.
This example shows comprehensive customer data processing using multiple string operations for cleaning, formatting, and validation.
123456789101112131415161718192021222324252627282930313233343536373839404142434445* Process CSV data with UNSTRING PROCESS-CSV-LINE. * Parse CSV line into fields UNSTRING WS-CSV-LINE DELIMITED BY "," INTO WS-FIELD1 DELIMITER IN WS-DELIM1, WS-FIELD2 DELIMITER IN WS-DELIM2, WS-FIELD3 DELIMITER IN WS-DELIM3, WS-FIELD4 DELIMITER IN WS-DELIM4 WITH POINTER WS-POSITION TALLYING IN WS-FIELD-COUNT ON OVERFLOW PERFORM HANDLE-CSV-OVERFLOW NOT ON OVERFLOW PERFORM VALIDATE-CSV-FIELDS END-UNSTRING. VALIDATE-CSV-FIELDS. * Clean each field PERFORM CLEAN-FIELD VARYING WS-I FROM 1 BY 1 UNTIL WS-I > WS-FIELD-COUNT. CLEAN-FIELD. EVALUATE WS-I WHEN 1 INSPECT WS-FIELD1 TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" END-INSPECT WHEN 2 INSPECT WS-FIELD2 TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" END-INSPECT WHEN 3 INSPECT WS-FIELD3 TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" END-INSPECT WHEN 4 INSPECT WS-FIELD4 TALLYING WS-SPACE-COUNT FOR LEADING SPACE REPLACING LEADING SPACE BY "0" END-INSPECT END-EVALUATE.
This example demonstrates parsing and cleaning CSV data using UNSTRING and INSPECT operations.
1234567891011121314151617181920212223242526272829* Generate formatted report lines GENERATE-REPORT-LINE. * Build header line STRING "CUSTOMER REPORT" DELIMITED BY SIZE, " - " DELIMITED BY SIZE, FUNCTION TRIM(WS-REPORT-DATE) DELIMITED BY SIZE, " - Page " DELIMITED BY SIZE, WS-PAGE-NUMBER DELIMITED BY SIZE INTO WS-HEADER-LINE END-STRING * Build detail line STRING WS-CUSTOMER-ID DELIMITED BY SIZE, " | " DELIMITED BY SIZE, FUNCTION TRIM(WS-CUSTOMER-NAME) DELIMITED BY SIZE, " | " DELIMITED BY SIZE, WS-CUSTOMER-AMOUNT DELIMITED BY SIZE, " | " DELIMITED BY SIZE, FUNCTION UPPER-CASE(WS-CUSTOMER-STATUS) DELIMITED BY SIZE INTO WS-DETAIL-LINE END-STRING * Build summary line STRING "TOTAL CUSTOMERS: " DELIMITED BY SIZE, WS-CUSTOMER-COUNT DELIMITED BY SIZE, " | TOTAL AMOUNT: $" DELIMITED BY SIZE, WS-TOTAL-AMOUNT DELIMITED BY SIZE INTO WS-SUMMARY-LINE END-STRING.
This example shows how string operations are used to format report lines with proper spacing and alignment.
Following these best practices ensures efficient and reliable string operations in COBOL programs.
1. What is the primary purpose of the STRING statement in COBOL?
2. Which statement is used to split a string into multiple parts?
3. What does the INSPECT statement do in COBOL?
4. What is the purpose of the DELIMITED BY clause in STRING and UNSTRING?
5. Which function returns the length of a string in COBOL?