Tips and Techniques: Trim Leading and Trailing Characters as Well as Blanks

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

In OS/400 V3R1, IBM introduced the %TRIM, %TRIML, and %TRIMR built-in functions. Effectively, these built-in functions delete trailing and/or leading blanks from a field and return a copy of the data without the trailing/leading blanks.

  • %TRIM deletes both trailing and leading blanks from a field and returns that value to the expression in which %TRIM is being used.
  • %TRIML deletes leading blanks (left side) from a field and returns the value to the expression in which %TRIML is being used.
  • %TRIM deletes trailing blanks (right side) from a field and returns the value to the expression in which %TRIMR is being used.

It would have been a lot more difficult to do certain things in RPG IV without these three built-in functions.

Nearly 10 years after RPG IV was announced and the trim functions were introduced, OS/400 V5R3 was announced, and it came with a second parameter for the trim functions. This new, second parameter allows you to identify specific characters you want to remove from the field. Whatever you specify for the second parameter is deleted from the field. As with the original trim functions, only the trailing or beginning characters are deleted, not those in the middle of the field.

Here's the new syntax:




%TRIMx( string-value  [ :  'trim-characters'] )

%TRIMx allows you to specify whether you want the characters removed from the beginning, the end, or both (%TRIML, %TRIMR, or %TRIM, respectively). The string-value parameter may be a field (including an array element), literal, named constant, or character-string expression. The optional trim-characters parameter may be one or more characters.

Remember, this enhancement only works on V5R3 or later, which means it is unavailable to virtually everyone as of this writing. Sorry. Cool idea, though. If you want to do this today, you can use the TRIML and TRIMR subprocedure that I wrote about in a previous issue. Then, when you move to V5R3, simply do a scan/replace by adding the % prefix to the subprocedure names. Or better yet, code this:

      /IF DEFINED(*V5R3M0)
     C                   eval      myVal = %TRIMR(CustName:' ."')
     C/ELSE 
     C                   eval      myVal = TRIMR(CustName:' ."')
     C/ENDIF

By using precompiler directives, you can prepare your code to use the contemporary features when they become available. Until then, you code a workaround that performs the equivalent routine.

Now get back to work!

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: