A COBOL substring uses reference modification. WS-NAME(3:5) is 5 characters starting at position 3. The first character is position 1. WS-NAME(5:) is from position 5 through the end of the field. Some compilers write the same slice as WS-NAME(3:5) and others as WS-NAME(3, 5). Check the manual for the comma form before you copy this into a program that will not compile it.
You can use the slice on either side of MOVE, in an IF, or as a STRING or UNSTRING field. The source field is not shortened. You are only naming part of it.
12345601 WS-FULL PIC X(20) VALUE 'SMITH,JOHN'. 01 WS-LAST PIC X(5). 01 WS-FIRST PIC X(4). PROCEDURE DIVISION. MOVE WS-FULL(1:5) TO WS-LAST MOVE WS-FULL(7:4) TO WS-FIRST
WS-LAST receives SMITH. WS-FIRST receives JOHN. The comma in position 6 is skipped.
String functions are built-in tools that do one job on a piece of text: "how long is it?" (LENGTH), "make it all capitals" (UPPER-CASE), "spell it backwards" (REVERSE), or "give me just this part" (substring). You call them by name with the text you want to work on, and you get back a result you can use in a MOVE, IF, or COMPUTE.
FUNCTION LENGTH takes one argument (an alphanumeric or national identifier or literal) and returns its length in character positions as an integer. For a data item, the length is the declared length (e.g. PIC X(30) gives 30), not the number of non-blank characters. Use LENGTH when you need the size of a field for loops, validation, or passing to another program.
12345678901 WS-NAME PIC X(30). 01 WS-LEN PIC 9(2). MOVE FUNCTION LENGTH(WS-NAME) TO WS-LEN *> WS-LEN is 30 IF FUNCTION LENGTH(INPUT-FIELD) > 80 DISPLAY 'Input too long' END-IF
The argument can be a literal: FUNCTION LENGTH("HELLO") returns 5. The result is numeric and can be used in arithmetic or comparisons.
FUNCTION UPPER-CASE(argument) returns a string with the same length as the argument and all alphabetic characters converted to uppercase. FUNCTION LOWER-CASE(argument) converts to lowercase. The argument is not modified; the function returns a new value. Use these for case-insensitive comparison (e.g. compare UPPER-CASE(WS-INPUT) with UPPER-CASE(WS-KEY)) or for normalizing data for display or storage.
12345678901 WS-CODE PIC X(5) VALUE 'Ab12X'. 01 WS-NORM PIC X(5). MOVE FUNCTION UPPER-CASE(WS-CODE) TO WS-NORM *> WS-NORM is 'AB12X' IF FUNCTION UPPER-CASE(WS-INPUT) = 'YES' PERFORM CONFIRM-PROCESS END-IF
Non-alphabetic characters (digits, spaces, symbols) are unchanged. The result must be moved to a field of the same length (or longer) or used in an expression that accepts that length.
FUNCTION REVERSE(argument) returns a string with the same characters in reverse order. The argument is alphanumeric (or national). Common uses include reversing a numeric string for processing or reversing text. The argument is not modified.
1234501 WS-DIGITS PIC X(5) VALUE '12345'. 01 WS-REV PIC X(5). MOVE FUNCTION REVERSE(WS-DIGITS) TO WS-REV *> WS-REV is '54321'
NUMVAL and NUMVAL-C convert an alphanumeric (often edited numeric) string to a numeric value. Useful when you read a formatted number (e.g. with commas or currency) and need to use it in arithmetic. Some compilers offer WHEN-COMPILED, CURRENT-DATE, and others that return character or numeric values. For a full list, see your compiler’s intrinsic function documentation or the intrinsic-functions tutorial.
| Function | Arguments | Returns | Typical use |
|---|---|---|---|
| LENGTH | One alphanumeric or national. | Integer length in characters. | Loop bounds, validation, buffer sizing. |
| UPPER-CASE | One alphanumeric. | Same length, uppercase. | Case-insensitive compare, display. |
| LOWER-CASE | One alphanumeric. | Same length, lowercase. | Normalization, display. |
| REVERSE | One alphanumeric. | Same length, reversed. | Reversing digits or text. |
| NUMVAL / NUMVAL-C | Alphanumeric (edited numeric). | Numeric value (float or integer). | Converting display numbers to numeric. |
Intrinsic functions are used in expressions: MOVE FUNCTION LENGTH(x) TO y, IF FUNCTION UPPER-CASE(a) = b, COMPUTE z = n + FUNCTION LENGTH(ws-field). The function reference is evaluated first; the result is then used in the statement. The result type (numeric or alphanumeric) must be compatible with where it is used. For STRING or UNSTRING, you can use reference modification to pick a slice of a field as source or target.
1. FUNCTION LENGTH(WS-FIELD) returns:
2. To get the 3rd through 7th character of WS-NAME you can use:
3. FUNCTION UPPER-CASE is used to: