Start of change

%TRIMR built-in function

The trim right built-in function (%TRIMR) with one parameter produces a character string with any trailing blanks removed. The trim right built-in function (%TRIMR) with two parameters produces a character string with any trailing characters that are in the characters to trim parameter removed. This function can only be used within a CL program or procedure.

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

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

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

  • Trim trailing blank characters. The trailing blank characters are trimmed from the CL variables &FIRSTNAME and &LASTNAME and the resulting strings are concatenated with the *CAT operator. The concatenated string is then assigned to CL variable &NAME.
    DCL VAR(&FIRSTNAME) TYPE(*CHAR) LEN(10) VALUE('JOHN      ') 
    DCL VAR(&LASTNAME) TYPE(*CHAR) LEN(10) VALUE('SMITH     ') 
    DCL VAR(&NAME) TYPE(*CHAR) LEN(10) 
    CHGVAR VAR(&NAME) VALUE(%TRIM(&FIRSTNAME) *CAT %TRIM(&LASTNAME)) 
  • Trim trailing characters that are specified in a literal string. All trailing zero and plus sign characters are trimmed from 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) LEN(10) VALUE('5.2700000+') 
    DCL VAR(&TRIMMED) TYPE(*CHAR) LEN(5) 
    DCL VAR(&DEC) TYPE(*DEC) LEN(3 2)  
    CHGVAR VAR(&TRIMMED) VALUE(%TRIMR(&PRICE '0+')) 
    CHGVAR VAR(&DEC) VALUE(&TRIMMED)
  • Trim trailing characters that are specified in a CL variable. Starting from 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(2) VALUE('* ') 
    IF COND(%TRIMR(&NAME &TCHAR) *EQ '12345') + 
       THEN(SNDPGMMSG ('EQUAL!'))
End of change