Use RPG-IV (aka RPG ILE)
shortfield = %TRIM(longfield)
You can also trim only Left or only Right, and you can specifiy what char to trim. The RPG manual (on the IBM website) has examples.
RPG/400 (OPM RPG) doesn’t have a “trim” function. It does, however, have a SUBST operation and other op-codes to help in determining the start and end of substrings.
CL doesn’t have a “trim” function. It generally doesn’t need one nor could it make much use of one except when concatenating strings. For that, use the *BCAT or *TCAT constructs. When using replacement data in message texts, the message description will perform automatic trims.
Minor addition — ILE CL (and any ILE language) can call the builtin ‘triml’ procedure to find the length of a “trimmed” variable. With the length known, the %SST() function gives an effective trim.
dcl &str *char 10 value( ‘abc’ )
dcl &stri *char 11
dcl &Trim *char 1 value( ‘ ‘ )
dcl &len *int value( 0 )
dcl &x00 *char 1 value( x’00′ )
chgvar &stri ( &str *cat &x00 )
callprc ‘triml’ ( +
( &Trim *byval ) +
rtnval( &len )
Note that a null terminator x’00′ is required. The length is returned as an *INT. Compile as a *MODULE, then use CRTPGM with BNDDIR(QC2LE) to find ‘triml’.
RPG IV (ILE RPG) uses the %trim() builtin function and its variants are as noted above.