String Handling: STRING, UNSTRING & Reference Modification

COBOL Fundamentals

Chapter 7 · String Handling: STRING, UNSTRING & Reference Modification

MOVE copies a whole field. Real COBOL programs constantly need something finer-grained — building one field out of several pieces, splitting one field apart into several, or reaching into the middle of a field without touching the rest. Four real verbs cover that: STRING, UNSTRING, reference modification, and INSPECT.

STRING: Real Concatenation

Extending Chapter 3's own real WS-CUSTOMER-RECORD, STRING builds one output field out of several source pieces at once — genuinely more direct than a chain of separate MOVE statements:

01 WS-REPORT-LINE PIC X(50). STRING "CUSTOMER: " DELIMITED BY SIZE WS-CUSTOMER-NAME DELIMITED BY SPACE " (ID: " DELIMITED BY SIZE WS-CUSTOMER-ID DELIMITED BY SIZE ")" DELIMITED BY SIZE INTO WS-REPORT-LINE END-STRING.

Each source piece gets its own real DELIMITED BY clause, and the two forms genuinely differ:

  • DELIMITED BY SIZE — transfers the entire sending field, exactly as declared, with no stopping point
  • DELIMITED BY SPACE (or any other literal/field) — stops the moment that real delimiter is reached, so a name field padded with trailing spaces doesn't drag its own padding into the result

A real optional POINTER phrase can control exactly where in the receiving field STRING starts writing:

STRING "MORE TEXT" DELIMITED BY SIZE INTO WS-REPORT-LINE WITH POINTER WS-POS END-STRING.
A Real, Documented Requirement Easy to Miss A POINTER field genuinely must be initialized to a real value before STRING ever runs — 1, to start writing from the very beginning of the receiving field. Skip that initialization and the pointer holds whatever value was already sitting in storage, so STRING genuinely starts writing from wherever that leftover value happens to point, silently overwriting the middle of the field instead of building it from the start.

UNSTRING: Real Splitting

UNSTRING does the reverse — one real source field split apart into several destination fields, wherever a chosen delimiter appears:

01 WS-FULL-NAME PIC X(30) VALUE "JANE MARY DOE". 01 WS-FIRST-NAME PIC X(15). 01 WS-MIDDLE-NAME PIC X(15). 01 WS-LAST-NAME PIC X(15). UNSTRING WS-FULL-NAME DELIMITED BY SPACE INTO WS-FIRST-NAME WS-MIDDLE-NAME WS-LAST-NAME END-UNSTRING.

Real DELIMITED BY treats each occurrence of the given delimiter as one real split point, checked left to right through the source field. Two further real phrases capture extra detail about each split as it happens:

  • DELIMITER IN — stores which real delimiter actually triggered each individual split (genuinely useful when more than one possible delimiter is allowed)
  • COUNT IN — stores the real number of characters that were examined for each individual destination field

Like STRING, UNSTRING also accepts a real WITH POINTER phrase to control where scanning of the source field begins.

Reference Modification: Real Substrings

Introduced in COBOL-85 — the same real standard that added Chapter 5's own EVALUATE and inline PERFORM — reference modification reaches directly into the middle of a field with no separate STRING or UNSTRING statement needed at all, using a real colon-separated syntax directly after the field's own name:

MOVE WS-CUSTOMER-NAME(1:5) TO WS-NAME-PREFIX.

(1:5) means "start at position 1, take 5 characters" — and, matching Chapter 6's own real 1-based subscripting, position 1 is genuinely the field's first real character, never a zeroth one. The length is real but optional — leaving it off extends the real reference all the way to the end of the field:

MOVE WS-CUSTOMER-NAME(6:) TO WS-NAME-REMAINDER.

INSPECT: Counting & Replacing

INSPECT examines a field's own real content without moving it anywhere — counting how often something appears (TALLYING) or swapping characters in place (REPLACING):

INSPECT WS-REPORT-LINE TALLYING WS-SPACE-COUNT FOR ALL SPACES.

A real REPLACING phrase substitutes matching characters, one for one, directly inside the existing field:

INSPECT WS-PHONE-NUMBER REPLACING ALL "-" BY SPACE.
Real Counting/Replacing Modes Both TALLYING and REPLACING accept real ALL (every occurrence), LEADING (only contiguous occurrences starting from the very beginning of the field), and — for REPLACING specifically — FIRST (only the single leftmost occurrence). TALLYING ... CHARACTERS counts every real character in the field regardless of content.

Hands-On Exercises

Exercise 1

Using this chapter's own real STRING example, explain in your own words the genuine difference between DELIMITED BY SIZE and DELIMITED BY SPACE, and why WS-CUSTOMER-NAME specifically uses the SPACE form rather than SIZE.

📄 View solution
Exercise 2

Using this chapter's own real warn-box, explain in your own words what genuinely happens if a program uses STRING ... WITH POINTER WS-POS but never initializes WS-POS to 1 beforehand, and why the resulting bug is a real, silent one rather than something COBOL would catch on its own.

📄 View solution
Exercise 3

Using this chapter's own real UNSTRING example, explain in your own words what would genuinely happen if WS-FULL-NAME held only two words instead of three (e.g. "JANE DOE"), given that UNSTRING has three destination fields waiting to receive values.

📄 View solution

Chapter 7 Quick Reference

  • STRING ... INTO — real concatenation; each source needs its own DELIMITED BY SIZE (whole field) or DELIMITED BY a stopping value
  • UNSTRING ... INTO — real splitting on a delimiter; DELIMITER IN/COUNT IN capture per-split detail
  • WITH POINTER (STRING/UNSTRING) — must be genuinely initialized (typically to 1) before use, or the starting position is whatever leftover value was already in storage
  • Reference modification (COBOL-85) — field(start:length); 1-based; length is real but optional, defaulting to the rest of the field
  • INSPECT ... TALLYING — real counting; INSPECT ... REPLACING — real in-place character substitution; both support ALL/LEADING/CHARACTERS