In this example, I will show you on how to use the various %trim built in functions

%trim – will remove the trailing blanks from end of text field.
%trimR – will remove trailing blanks or specified characters from end of text field.
%trimL – will remove specified characters or blanks from beginning of text field.

Due to the length of my fields, I am using IBM QUILNGTX to display the text. This is not required for using the %trim BIF.

Trim trailing blanks only one parameter passed

In this example will remove only trailing blanks

Text = (‘First Name: ‘ + %trim(FirstName)) ;

Trim extra characters at end two parameters passed

In this example, will remove * and any blank characters

Text = (‘First Name: ‘ + %trimR(FirstNameR :’* ‘)) ;

Trim extra characters at beginning two parameters passed

In this example will remove $, * and any blank characters

Text = (‘Check Numeric Amount : ‘ + %trimL(CheckAmtL : ‘$* ‘)) ;

Example of data after utilizing %trim BIF

Programming code example

free ctl-opt dftactgrp(no) actgrp(new); dcl-s FirstName char(35) Inz(‘Donald ‘) ; dcl-s LastName char(35) Inz(‘Duck ‘) ; dcl-s FirstNameR char(35) Inz(‘Mickey‘) ;
dcl-s LastNameR char(35) Inz(‘Mouse####’) ;
dcl-s CheckAmtL char(35) Inz(‘$
12.33′) ;
dcl-s Text char(150) ;
dcl-s Len int(10) ;

// Will trim trailing blanks, only one parameter being passed for First Name
Text = (‘First Name: ‘ + %trim(FirstName)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// Will trim trailing blanks, only one parameter being passed for Last Name
Text = (‘Last Name: ‘ + %trim(LastName)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// Will trim trailing blanks, only one parameter being passed for Each Name
Text = (‘Full Name : ‘ + %trim(FirstName) + ‘ ‘ + %trim(LastName)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// TrimR (Trim right) and remove * at end
Text = (‘First Name: ‘ + %trimR(FirstNameR :’* ‘)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// TrimR (trim right) and remove # at end
Text = (‘Last Name: ‘ + %trimR(LastNameR : ‘# ‘)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// TrimR (trim right) and remove * and # at end of each field and concatenate
Text = (‘Full Name : ‘ + %trimR(FirstNameR : ‘* ‘) + ‘ ‘ + %trimR(LastNameR : ‘# ‘)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
// TrimL (trim left) wo;; remove $* from begining of text to display number
Text = (‘Check Numeric Amount : ‘ + %trimL(CheckAmtL : ‘$* ‘)) ;
Len = %len(%trim(Text)) ;
display_Window (Text) ;
*inlr = *on ;

// ======================================================================
// Display window prototype
// Use IBM i API ‘QUILNGTX’ to dynamically show messages in a
// window format rather than on the error message line.
// ====================================================================== dcl-proc Display_Window export; dcl-pi Display_Window ; p_Text varchar(8192) const; p_MsgId char(7) Options(nopass:omit); p_MsgFile char(21) Options(nopass:*omit);
end-pi;
dcl-ds myApiError inz qualified;
Bytes int(10) inz(%size(myApiError));
BytesAvailable int(10) inz;
ErrorID char(7) inz;
MessageData char(128) inz;
end-ds;

dcl-pr QUILNGTX extpgm(‘QUILNGTX’);
*n char(8192) const; // MsgText
*n int(10) const; // MsgLength
*n char(7) const; // MessageId
*n char(21) const; // MessageFile
*n options( *omit: *varsize ) like( MYapierror ); // ErrorDS
end-pr;
Dcl-S Msgid like(p_MsgId) ;
Dcl-S MsgFile like(p_MsgFile) ;
If %Parms = 1;
MsgId = ‘CAE0103’; // ‘Press Enter to continue.’
MsgFile = ‘QCPFMSG *LIBL’;
Elseif %Parms = 2;
MsgId = p_MsgId;
MsgFile = ‘QCPFMSG *LIBL’;
Elseif %Parms = 3;
MsgId = p_MsgId;
MsgFile = p_MsgFile;
Endif;

// Call the program
QUILNGTX(Text : len : MsgId : MsgFile : MyApiError) ;
End-Proc ;

Posted in

Leave a Reply