In the next release of RPG IV, the %TRIM, %TRIMR, and %TRIML built-in functions support a second parameter that allows you to specify the character or characters that you want to remove. Without this capability, you can only trim off the trailing or leading blanks, which is useful for probably 80-90% of the situations, but it's occasionally restrictive.
The RPG ToolKit includes two subprocedures that you can call to perform the same functionality as the upcoming RPG IV built-in function enhancements. That is, they support a second parameter that can accept one or more characters to be trimmed off the end or the beginning of the data. One advantage these procedures have over the new RPG IV enhancements is that they can be used as far back as V4R5, so you don't have to worry about what release of OS/400 you're running.
If you have the RPG ToolKit, you can include the UTILS member in your RPG IV program to call triml() or trim(). If you don't have the ToolKit, I've included the source code for TRIMR below.
D trimR PR 4096A Varying
D szInStg 4096A Const Varying
D szTrimPat 256A Const Varying
** Implementation of the TRIMR() subprocedure
P trimR B Export
D trimR PI 4096A Varying
D szInStg 4096A Const Varying
D szTrimPat 256A Const Varying
D szTrim S 4096A Varying
D nEnd S 10I 0
C szTrimPat CheckR szInStg nEnd
** If nothing is returned, we return an empty string.
C if NOT %Found
C eval szTrim = ''
C else
C eval szTrim = %subst(szInStg:1:nEnd)
C endif
C return szTrim
P TrimR E
The TRIMR and TRIML subprocedures are two of over 15 subprocedures in the ToolKit that do not require a license key to use; they are part of the evaluation library. That is, they will work even if you don't have the licensed ToolKit. You can download the evaluation library at www.rpglib.com.
Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.
LATEST COMMENTS
MC Press Online