Sign in

docs RPG Reference

%TRIMR (Trim Trailing Characters)

%TRIMR (Trim Trailing Characters)

%TRIMR(string {: characters to trim {: *NATURAL | *STDCHARSIZE}})

%TRIMR with only one parameter returns the given string with any trailing blanks removed.

%TRIMR with two parameters returns the given string with any trailing characters that are in the characters to trim parameter removed.

The string can be character, graphic, or UCS-2 data.

If the characters to trim parameter is specified, and it does not have the same type and CCSID as the string parameter, it is converted to the type and CCSID of the string parameter.

The second or third parameter can be *NATURAL or *STDCHARSIZE to override the current CHARCOUNT mode for the statement. If this parameter is specified, it must be the last parameter.

For information about the CHARCOUNT mode affects %TRIMR, see %TRIM with CHARCOUNT NATURAL.

When specified as a parameter for a definition specification keyword, the string parameter must be a constant.

Note: Specifying %TRIMR with two parameters is not supported for parameters of Definition keywords.

For more information, see String Operations or Built-in Functions.

Figure 1. %TRIMR Example

 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
D*Name++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D Location        S             16A   varying
D FirstName       S             10A   inz ('Chris')
D LastName        S             10A   inz ('Smith')
D Name            S             20A   varying

     // LOCATION will have the value '  Toronto, Ontario'.
     Location = %trimr ('  Toronto, Ontario  ');
     // Name will have the value 'Chris Smith:'.
     Name = %trimr (FirstName) + ' ' + %trimr (LastName) + ':';

Figure 2. Trimming characters other than blanks

     string = '(' + %trimr('$******5.27***      ' : '$*') + ')';
     // string is now '($******5.27***      )'
     //
     // Nothing has been trimmed from the right-hand side because
     // the right-most character is a blank, and a blank does not
     // appear in the 'characters to trim' parameter

     string = '(' + %trimr('$******5.27***      ' : '$ *') + ')';
     // string is now '($******5.27)'