Handling null-terminated strings

COBOL supports null-terminated strings when you use string-handling statements together with null-terminated literals and the hexadecimal literal X'00'.

About this task

You can manipulate null-terminated strings (passed from a C program, for example) by using string-handling mechanisms such as those in the following code:


01 L        pic X(20) value z'ab'.
01 M        pic X(20) value z'cd'.
01 N        pic X(20).
01 N-Length pic 99    value zero.
01 Y        pic X(13) value 'Hello, World!'.

To determine the length of a null-terminated string, and display the value of the string and its length, code:


Inspect N tallying N-length for characters before initial X'00'
Display 'N: ' N(1:N-length) ' Length: ' N-length

To move a null-terminated string to an alphanumeric string, but delete the null, code:


Unstring N  delimited by X'00' into X

To create a null-terminated string, code:


String Y      delimited by size
       X'00'  delimited by size
       into N.

To concatenate two null-terminated strings, code:


String L      delimited by x'00'
       M      delimited by x'00'
       X'00'  delimited by size
       into N.

Related references  
Null-terminated alphanumeric literals
(COBOL for Linux® on x86 Language Reference)