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

Is there an easy way to directly return the length of a character and a type variable in RPGLE? The length I'm talking about here is not the length specified in the D-spec. I am talking about the actual number of significant characters in a string. Let them say that a character type variable has a length of 50 characters and is assigned the value "Hello world!", Then the length I want is 12, from "H" to "!". Top and end nulls are ignored. Is there an easy way to do this?

+4
source share
2 answers

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

  • %triml aligns leading spaces (left)
  • %trimr trims trailing spaces (right)
  • %trim aligns leading and trailing spaces
+11
source

It looks like the field you are trying to find the length in is a fixed character field, like

  dmsg s 40a 

If we do eval msg = 'Hello, World!' then msg does not contain "Hello, World!" - it contains "Hello world!" That is, he has a bunch of spaces to append to 40 characters. How fixed-length fields work by definition.

% trimr () can work very well with them, and even has an optional parameter to determine which characters to trim.

On the other hand, if you must use a variable length field

  dmsg s 40a Varying 

and then did eval msg = 'Hello, World!' that field actually contains only the characters assigned to it. In this case, there is no% trimr (); % len () will return the current field length.

+8
source

Source: https://habr.com/ru/post/1380603/


All Articles