What is String concatenation in COBOL.
Read my previous post on COBOL Strings-INSPECT VERB.
String concatenation involves joining the contents of two or more source strings or partial source strings to create a single destination string. In COBOL, string concatenation is done using the STRING verb. Before I discuss the STRING verb formally, let’s look at some examples to get a feel for what it can do.
STRING String1, String2, “LM051” DELIMITED BY SIZE INTO DestString END-STRING STRING String1 DELIMITED BY SIZE String2 DELIMITED BY SPACES String3 DELIMITED BY “unique” INTO DestString END-STRING
Using Pointer in String
STRING MonthStr DELIMITED BY SPACES "," DELIMITED BY SIZE INTO DateStr WITH POINTER StrPtr
Key points in COBOL strings
- The ON OVERFLOW clause executes if valid characters remain to be transferred in the source string but the destination string is full.
- When a WITH POINTER phrase is used, its value determines the starting character position for insertion into the destination string. As each character is inserted into the destination string, the pointer is incremented. When the pointer points beyond the end of the destination string, the STRING statement stops.
- When the WITH POINTER phrase is used, then before the STRING statement executes, the program must set Pointer#i to an initial value greater than zero and less than the length of the destination string.
- If the WITH POINTER phrase is not used, operation on the destination field starts from the leftmost position.
- Pointer#i must be an integer item, and its description must allow it to contain a value one greater than the size of the destination string. For instance, a pointer declared as PIC 9 is too small if the destination string is ten characters long.
- The DELIMITED BY SIZE clause causes the whole of the sending field to be added to the destination string. Where a literal can be used, you can use a figurative constant (such as SPACES ) except for the ALL literal figurative constant.When a figurative constant is used, it is one character in size.
- The destination item DestString must be either an elementary data item without editing symbols or the JUSTIFIED clause.
Data movement from a particular source string ends when one of the following occurs:
- The end of the source string is reached.
- The end of the destination string is reached.
- The delimiter is detected.
- The STRING statement ends when one of the following is true:
- All the source strings have been processed.
- The destination string is full.
- The pointer points outside the string.
SPLITTING of String
String splitting involves chopping a string into a number of smaller strings. In COBOL, string splitting is done using the UNSTRING verb. Before I discuss the UNSTRING verb formally, let’s look at some examples to see what UNSTRING can do.
UNSTRING CustomerName DELIMITED BY ALL SPACES INTO FirstName, SecondName, Surname END-UNSTRING
The second example breaks an address string
UNSTRING CustAddress DELIMITED BY "," INTO AdrLine(1), AdrLine(2), AdrLine(3), AdrLine(4), AdrLine(5), AdrLine(6) TALLYING IN AdrLinesUsed END-UNSTRING