%TRIM built-in function

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

The %TRIM built-in function can be used anywhere that CL supports a character expression. %TRIM can be used alone or as part of a more complex character expression. For example, %TRIM can be used to compare to a character CL variable in the COND parameter of an IF or WHEN command. %TRIM 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 built-in function is:
%TRIM(character-variable-name [characters-to-trim])

The trim 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 function produces a string of blank characters.

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

  • Trim leading and trailing blank characters. The leading and trailing blanks are trimmed from the CL variables &FIRSTNAME and &LASTNAME and the resulting strings are concatenated with a single blank between the two values with the *BCAT operator. The concatenated string is then assigned to CL variable &NAME.
    DCL VAR(&FIRSTNAME) TYPE(*CHAR) VALUE('   JOHN  ') 
    DCL VAR(&LASTNAME) TYPE(*CHAR) VALUE('   SMITH  ') 
    DCL VAR(&NAME) TYPE(*CHAR) LEN(10) 
    CHGVAR VAR(&NAME) VALUE(%TRIM(&FIRSTNAME) *BCAT %TRIM(&LASTNAME)) 
  • Trim characters that are specified in a literal string. All asterisks and blanks are trimmed from the beginning and end of CL variable &NUM and the remaining characters (1.23) are assigned to CL variable &TRIMMED. The character variable &TRIMMED is converted to a numeric value and assigned to decimal variable &DEC.
    DCL VAR(&NUM) TYPE(*CHAR) LEN(10) VALUE('* *1.23* *') 
    DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(10) 
    DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)  
    CHGVAR &TRIMMED %TRIM(&NUM '* ') 
    CHGVAR VAR(&DEC) VALUE(&TRIMMED)
  • Trim 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. Also, starting with the last 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(3) VALUE('*+ ') 
    IF COND(%TRIM(&NAME &TCHAR) *EQ '12345') + 
       THEN(SNDPGMMSG ('EQUAL!'))