Previous Topic Next topic Print topic


Character-String Data

A character-string value is a sequence of characters. The number of characters in a sequence is called the length of the sequence. In Open PL/I, the maximum length of a string value is 32767 bytes or characters.

A character-string of zero length is called a null string.

A character-string variable or character-string valued function is declared with the following attributes:

CHARACTER(n)

or

CHARACTER(n) VARYING

or

CHARACTER(n) VARYINGZ

where n is an integer valued expression that specifies the maximum length of all string values that can be held by the variable or returned by the function.

The VARYING (VAR) attribute causes the string variable or function to hold or return values of varying lengths. Internally, the length of the varying string is recorded along with the value. Varying strings are not padded to assume their maximum length, n. The representation of a varying string variable in storage is such that any string up to n characters may be held by the variable, and the length of the current string is retained as part of the value.

The VARYINGZ (VARZ) attribute causes the string variable or function to hold or return values of varying lengths. Internally, the varyingz strings are a stored as a sequence of characters terminated by a ‘00’x byte, and are not padded to assume their maximum length, n. The representation of a varying string variable in storage is such that any string up to n characters may be held by the variable.

Without the VARYING attribute, a string variable or function always holds or returns values of length n. An assignment to a nonvarying string always extends short values with blanks on the right to make them n characters long.

Assignments of a string of more than n characters to either a varying or a nonvarying string variable cause only the leftmost n characters to be assigned and excess characters to be truncated.

Character-string values are compared from left to right using the collating sequence of the computer. Strings of unequal length are compared by effectively extending the shorter string with blanks on the right.

Nonvarying character-string variables always occupy exactly n bytes of storage. As elements of arrays or members of a structure, they begin on the next available byte and are not aligned on word or other storage address boundaries. This permits an array of nonvarying characters to be stored and accessed as if it were a single string. See the section Storage Sharing in the chapter Storage Classes.

Varying character-string variables always occupy n+2 bytes of storage. The first 2 bytes contain an integer L (0 <= L <= n) that specifies the length of the string value currently stored in the variable. The string text occupies the first L bytes of the storage following the 2 length bytes. The value of the last n–L bytes of the variable is undefined. The value L can be accessed using the LENGTH built-in function. An array of varying character strings cannot be accessed as if it were a single character string.

Varyingz character-string variables always occupy n+1 bytes of storage. The current length L of a varyingz string is determined by the position of the first ‘00’x byte, where L = index(string, ‘00x’) – 1. The string text occupies the first L bytes of the storage following the ‘00’x terminator. The value of the last n–L bytes of the variable is undefined. The value L can be accessed using the LENGTH built-in function. An array of varyingz character strings cannot be accessed as if it were a single character string.

See the Open PL/I User's Guide for specific alignment of varying character strings.

A character-string constant is written in the following format:

'Any characters except quote'

If an apostrophe is required within the constant, it must be written as an adjacent pair of apostrophes.

Examples:

'ABC'
'He said, "I don"t know."'
''

In these examples, the second string displays a pair of single quotation marks used within the text string "don't". This text string also shows that the double quotation mark is insignificant as a delimiter of a character-string constant. The string in the last example is a null string.

Note: Any string produced as an intermediate result requires storage allocated either on the stack. System area storage is required for the ALLOCATE statement. Allocating a large temporary storage block that exceeds the amount of available storage results in a signal of the ERROR condition.
Previous Topic Next topic Print topic