The STRING Statement
STRING concatenates two or more source fields or literals into a single receiving field, with each source optionally qualified by a DELIMITED BY clause that controls how much of that source is copied: DELIMITED BY SIZE copies the entire field including trailing spaces, while DELIMITED BY SPACE or a specific literal copies only up to the first occurrence of that delimiter, which is essential for concatenating fixed-length alphanumeric fields without pulling in unwanted padding. A WITH POINTER phrase lets the program control exactly where in the receiving field the next piece gets written, which is necessary when building a string across multiple STRING statements or when starting the concatenation partway through an already-populated field. If the receiving field is too small to hold everything being strung together, the excess characters are silently discarded unless an ON OVERFLOW clause is present to catch the condition.
Cricket analogy: STRING WS-PLAYER-NAME DELIMITED BY SPACE " - " DELIMITED BY SIZE WS-TEAM DELIMITED BY SPACE INTO WS-COMMENTARY-LINE mirrors a scorecard generator assembling 'Kohli - India' by trimming trailing spaces from each fixed-width name field before joining them.
01 WS-FIRST-NAME PIC X(15) VALUE "JOHN".
01 WS-LAST-NAME PIC X(15) VALUE "SMITH".
01 WS-FULL-NAME PIC X(31).
PROCEDURE DIVISION.
STRING WS-FIRST-NAME DELIMITED BY SPACE
" " DELIMITED BY SIZE
WS-LAST-NAME DELIMITED BY SPACE
INTO WS-FULL-NAME
ON OVERFLOW
DISPLAY "NAME TOO LONG FOR FIELD"
END-STRING.
DISPLAY "FULL NAME: " WS-FULL-NAME.The UNSTRING Statement
UNSTRING performs the inverse operation of STRING, splitting a single source field into multiple receiving fields based on one or more DELIMITED BY separators, such as a comma or a specific literal, and is the standard COBOL technique for parsing delimited input like a CSV record without writing manual character-by-character scanning logic. Each receiving field in the INTO clause can optionally have a matching DELIMITER IN and COUNT IN field to capture which delimiter was actually matched and how many characters were placed into that receiving field, which is useful when the input format is not perfectly uniform. Like STRING, UNSTRING supports WITH POINTER to control the starting scan position and TALLYING to count how many receiving fields were populated, and it silently stops filling fields once either the source is exhausted or all receiving fields are used, so the ON OVERFLOW clause should be checked whenever the input might contain more delimited pieces than there are receiving fields.
Cricket analogy: UNSTRING WS-SCORE-LINE DELIMITED BY "/" INTO WS-RUNS WS-WICKETS mirrors parsing a scoreline like '287/6' from a live feed into separate runs and wickets fields for a scoreboard app.
01 WS-CSV-LINE PIC X(40) VALUE "1024,SMITH,ENGINEERING".
01 WS-EMP-ID PIC X(6).
01 WS-EMP-LNAME PIC X(15).
01 WS-EMP-DEPT PIC X(15).
PROCEDURE DIVISION.
UNSTRING WS-CSV-LINE DELIMITED BY ","
INTO WS-EMP-ID
WS-EMP-LNAME
WS-EMP-DEPT
ON OVERFLOW
DISPLAY "MORE FIELDS THAN RECEIVING ITEMS"
END-UNSTRING.INSPECT TALLYING
INSPECT WS-FIELD TALLYING WS-COUNT FOR ALL "A" counts occurrences of a character or substring within a field without modifying the field itself, which is the standard COBOL idiom for validation tasks like counting how many non-space characters a field contains, or how many times a delimiter appears before deciding how to parse it with UNSTRING. TALLYING supports several counting modes: FOR ALL counts every occurrence, FOR LEADING counts only a contiguous run at the start of the field, and FOR CHARACTERS counts every single character position regardless of value, and multiple TALLYING phrases can appear in one INSPECT statement to gather several counts in a single pass over the field. Because INSPECT TALLYING scans the field exactly once regardless of how many counting phrases are specified, it's generally more efficient than writing an equivalent PERFORM VARYING loop that examines each character position manually.
Cricket analogy: INSPECT WS-OVER-STRING TALLYING WS-DOT-COUNT FOR ALL "." mirrors a scorer counting dot balls in an over's notation string, tallying every '.' character to compute the over's economy in one pass.
INSPECT REPLACING and CONVERTING
INSPECT WS-FIELD REPLACING ALL "0" BY "O" modifies a field in place by substituting every occurrence of one character or substring with another, following the same ALL, LEADING, and FIRST qualifiers available to TALLYING, and a single INSPECT statement can combine TALLYING and REPLACING phrases to count and modify a field in one pass. INSPECT ... CONVERTING is a more restricted but often more convenient form for simple character-set translation, such as converting all lowercase letters to uppercase with INSPECT WS-FIELD CONVERTING "abcdefghijklmnopqrstuvwxyz" TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ", which maps each character in the first literal to the corresponding character at the same position in the second literal across the entire field in a single statement. Unlike STRING and UNSTRING, INSPECT never changes the length of a field: REPLACING and CONVERTING only substitute characters position-for-position within the field's existing fixed size, which makes INSPECT unsuitable for any transformation that would need to insert or remove characters.
Cricket analogy: INSPECT WS-VENUE REPLACING ALL "STADIUM" BY "GROUND " mirrors a scorecard system standardizing venue names by swapping one term for another of the same length, without changing the overall field size.
Because INSPECT operates in place and never changes a field's length, it is the right tool for character-level substitution or counting on a fixed-size field, while STRING and UNSTRING are the right tools whenever the operation needs to combine multiple fields or split content into pieces whose lengths vary. Combining TALLYING and REPLACING in one INSPECT statement (for example counting how many substitutions occurred while making them) is a common pattern for validation routines that need to both clean and audit input data in a single pass.
- STRING concatenates multiple sources into one field, with DELIMITED BY SIZE or SPACE controlling how much of each source is copied.
- ON OVERFLOW on STRING and UNSTRING catches the case where the receiving field(s) can't hold all the data.
- UNSTRING splits one source field into multiple receiving fields based on DELIMITED BY separators, the standard way to parse CSV-style input.
- INSPECT TALLYING counts occurrences of a character or substring (ALL, LEADING, or CHARACTERS) without modifying the field.
- INSPECT REPLACING substitutes characters or substrings in place, following the same ALL/LEADING/FIRST qualifiers.
- INSPECT CONVERTING performs position-mapped character-set translation, such as case conversion, in a single statement.
- INSPECT never changes a field's length; STRING and UNSTRING are needed whenever length needs to change.
Practice what you learned
1. What is the effect of DELIMITED BY SPACE versus DELIMITED BY SIZE in a STRING statement?
2. What does the ON OVERFLOW clause in STRING or UNSTRING detect?
3. Which INSPECT phrase counts occurrences of a character or substring without modifying the field?
4. Why is INSPECT unsuitable for a transformation that needs to insert or remove characters from a field?
Was this page helpful?
You May Also Like
MOVE and Arithmetic Statements
Learn how COBOL's MOVE statement reformats data between fields and how ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE perform arithmetic with proper rounding and overflow handling.
Conditional Logic in COBOL
Master COBOL's IF/ELSE, nested conditions, the EVALUATE statement, and condition-names (88-levels) for expressing readable business rules.
PERFORM Statement Variations
Explore the many forms of COBOL's PERFORM statement, from simple out-of-line calls and PERFORM THRU to TIMES, UNTIL, and in-line PERFORM blocks.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics