RPGLE中如何获取角色类型变量的长度?

发布于 2024-12-14 18:42:29 字数 173 浏览 8 评论 0原文

RPGLE中有没有简单的方法可以直接返回字符和类型变量的长度?我这里所说的长度并不是D-spec中规定的长度。我说的是字符串中有意义的字符的实际数量。假设一个字符类型变量定义为50个字符长,并为其赋值“Hello world!”,那么我想要的长度是12,即从“H”到“!”。前导和尾随空白将被忽略。有什么简单的方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花辞树 2024-12-21 18:42:31

您可以使用 %len(%trimr(field)),它会在检查长度之前修剪尾随空格。

  • %triml 修剪前导空格(左侧)
  • %trimr 修剪尾随空格(右侧)
  • %trim 修剪前导和尾随空格

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
愛放△進行李 2024-12-21 18:42:31

看起来您要查找长度的字段是固定字符字段,例如

     dmsg              s             40a

如果我们执行 eval msg = 'Hello, World!'那么 msg 不包含“Hello, World!” - 它包含“你好,世界!” ' 也就是说,它有一堆空白来将其填充到 40 个字符。根据定义,这就是固定长度字段的工作方式。

%trimr() 可以很好地处理这些字符,它甚至有一个可选参数来定义应该修剪哪些字符。

另一方面,如果您要使用可变长度字段

     dmsg              s             40a   Varying

,然后执行 eval msg = 'Hello, World!'那么该字段实际上只包含分配给它的字符。在这种情况下,不需要%trimr(); %len() 将返回字段的当前长度。

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

     dmsg              s             40a

If we do an eval msg = 'Hello, World!' then msg does not contain 'Hello, World!' - it contains 'Hello, World! ' That is, it has a pile of blanks to pad it out to 40 characters. That's how fixed length fields work by definition.

%trimr() can work very well with these, and it even has an optional parameter to define which characters should be trimmed.

On the other hand, if you were to use a varying length field

     dmsg              s             40a   Varying

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文