Key Takeaways — Chapter 17: String Handling
-
COBOL has four string handling facilities, each with a distinct purpose. STRING concatenates, UNSTRING parses, INSPECT examines character by character, and reference modification provides direct substring access.
-
DELIMITED BY controls what STRING uses. DELIMITED BY SPACE stops at the first space (problematic for multi-word values). DELIMITED BY SIZE uses the entire field. Choose the delimiter that matches your data.
-
Always initialize the POINTER before STRING. The STRING statement does not reset the pointer to 1 automatically. Forgetting to initialize causes data to be placed at unexpected positions.
-
UNSTRING's TALLYING IN must be zeroed manually. Unlike many counters, TALLYING IN accumulates rather than resets. Always MOVE ZERO to the tally variable before each UNSTRING.
-
INSPECT CONVERTING is the most efficient character translation. For case conversion, character substitution, or character set mapping, INSPECT CONVERTING with equal-length FROM/TO strings is both clean and fast.
-
Reference modification uses 1-based positioning. The first character is position 1, not 0. The syntax is identifier(offset:length), where length is optional.
-
Always validate reference modification bounds. Accessing beyond a field's defined length causes undefined behavior (potentially S0C4 ABEND). Check that offset > 0 and offset + length - 1 <= field length.
-
Use ON OVERFLOW with every STRING and UNSTRING in production. Silent truncation and silent extra-field conditions are data integrity risks. Log overflows at minimum; abort processing if data quality warrants it.
-
Detect the format before parsing. Use INSPECT TALLYING to characterize input (count delimiters, check for patterns) before committing to a specific UNSTRING parsing strategy.
-
Choose STRING vs. reference modification based on context. STRING is more readable for variable-length concatenation. Reference modification with MOVE is faster for fixed-position field placement in high-volume loops.