%TRIML built-in function

The trim left built-in function (%TRIML) with one parameter produces a character string with any leading blanks removed. The trim left built-in function (%TRIML) with two parameters produces a character string with any leading characters that are in the characters to trim parameter removed.

The %TRIML built-in function can be used anywhere that CL supports a character expression. %TRIML can be used alone or as part of a more complex character expression. For example, %TRIML can be used to compare to a character CL variable in the COND parameter of an IF or WHEN command. %TRIML can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *CHAR, *NAME, *SNAME, *CNAME, *PNAME, *GENERIC, *DATE, *TIME, or *X.

The format of the trim left built-in function is:
%TRIML(character-variable-name [characters-to-trim])

The trim left function produces a substring from the contents of the specified CL character variable. If the characters-to-trim parameter is specified, it must be either a CL character variable or a character literal. If, after trimming, no characters are left, the trim left function produces a string of blank characters.

The following examples are about the trim left built-in function:

  • Trim leading blank characters. The leading blanks are trimmed from the CL variable &NUMCHAR and the resulting string is converted to a numeric value and assigned to CL variable &NUMDEC.
    DCL VAR(&NUMCHAR) TYPE(*CHAR) VALUE('     00001') 
    DCL VAR(&NUMDEC) TYPE(*DEC) LEN(5) 
    CHGVAR VAR(&NUMDEC) VALUE(%TRIML(&NUMCHAR)) 
  • Trim leading characters that are specified in a literal string. All dollar signs and blanks are trimmed from the beginning of CL variable &PRICE and the remaining characters (5.27) are assigned to CL variable &TRIMMED. The character variable &TRIMMED is converted to a numeric value and assigned to decimal variable &DEC.
    DCL VAR(&PRICE) TYPE(*CHAR) VALUE('     $5.27') 
    DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(5) 
    DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)  
    CHGVAR VAR(&TRIMMED) VALUE(%TRIML(&PRICE '$ ')) 
    CHGVAR VAR(&DEC) VALUE(&TRIMMED)
  • Trim leading characters that are specified in a CL variable. Starting from the first character in variable &NAME, trim the character if it matches one of the characters in variable &TCHAR. The resulting substring of &NAME is compared to the literal string ’12345’ and a message is sent if the values are equal.
    DCL VAR(&NAME) TYPE(*CHAR) LEN(10) VALUE('    +12345') 
    DCL VAR(&TCHAR) TYPE(*CHAR) LEN(2) VALUE('+ ') 
    IF COND(%TRIML(&NAME &TCHAR) *EQ '12345') + 
       THEN(SNDPGMMSG ('EQUAL!'))