How to get the length of character type variable in RPGLE?

God_of_Thunder picture God_of_Thunder · Nov 10, 2011 · Viewed 12.1k times · Source

Is there any easy way to directly return the length of character and type variable in RPGLE? The length I am talking about here is not the length specified in the D-spec. I am talking about the actual number of meaningful characters in a string. Let's say a character type variable is defined to be 50 characters long, and it is assigned with value 'Hello world!', then the length I want is 12, which is from 'H' to '!'. The leading and trailing blank is ignored. Is there any simple way to do this?

Answer

dmc picture dmc · Nov 10, 2011

You can use %len(%trimr(field)), which trims trailing spaces before checking the length.

  • %triml trims leading spaces (on the left)
  • %trimr trims trailing spaces (on the right)
  • %trim trims leading and trailing spaces