在 RPGLE 中使用 String.format
我想将 RPGLE 与 String.format 接口,它接受可变长度参数或数组,我还想传递数字和字符串,所以我将使用类似 “hello %s,你已经 %d 年了”的格式旧”
。有人可以给我一些关于如何在 RPGLE 中制作原型的建议吗?
更新
似乎有些人对这个问题感到困惑。为了清楚起见,我想在 RPGLE 中制作以下原型。请注意,该方法的第二个参数是一个 varargs
参数,因此可以提供任意数量的参数! RPGLE 绝对不支持这一点,但它确实支持 *nopass 所以这对实现我需要的结果很有帮助。
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
or
String.format(format, "FirstName", "Init.", "LastName");
我对如何在 RPGLE 中格式化字符串不感兴趣,我想制作一个 java 方法的原型。
I would like to interface RPGLE with String.format which takes variable length arguments or an array, I also want to pass numbers as well as strings, so I will be using format like "hello %s, you are %d years old"
. Can someone give me some advide on how to prototype this in RPGLE?
UPDATE
It seems that some people were confused with the quesion. To make things clear, I want to prototype the following in RPGLE. Note that the second argument to the method is a varargs
parameter, so any number of arguments can be supplied! RPGLE definitely does not support this, but it does support *nopass
so this my be helpful in achieving the result I need.
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
or
String.format(format, "FirstName", "Init.", "LastName");
I am not interested in how I can format strings in RPGLE, I want to prototype a java method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用消息进行格式化怎么样?它们非常强大,并且文本是外部化的(并且 CCSID 感知)。
您可以使用 QMHRTVM API 检索格式化消息。
当您使用 RPG 代码时,调用本机功能总是比 Java 更快。
How about using message's to do the formatting ... they are quite powerful and the text is externalized (and CCSID aware).
You can use the QMHRTVM API to retrieve the formatted message.
When you're in RPG code, it's always faster to invoke native functionality than Java.
您想使用 Java 有什么特殊原因吗?启动 JVM 的开销对于许多应用程序来说可能是致命的。 RPG 本身可以使用串联轻松做到这一点。
Is there a particular reason you want to use Java? The overhead of starting up a JVM can be killer on many applications. RPG itself can do that easily using concatenation.
我完全同意我们更喜欢尽可能使用原生 RPGLE 函数。然而,作为 RPGLE 开发者,我们也愿意根据需要使用任何外部函数。
下面是一个可能与您的情况相关的示例,但我不确定它是否直接解决您的情况:
步骤 1. 使用接受格式字符串和可变参数参数的方法创建一个 Java 类:
步骤 2. 在 RPGLE 中程序中,定义 Java 方法的原型。您需要使用 *JAVA 和 *NOPASS 选项来处理可变参数。此原型允许您传递最多 10 个参数,这对于大多数情况来说应该足够了。如果您需要更多,可以使用其他参数扩展原型。最后,您可以从 RPGLE 程序中调用 formatString 方法,如下所示
I totally agree that we prefer using native RPGLE functions whenever possible. However, as RPGLE developers, we're also open to utilising any external functions as needed.
Here's an example that might be relevant to your situation, though I'm not sure if it directly addresses your case:
Step 1. Create a Java class with a method that accepts a format string and a varargs parameter:
Step 2. In the RPGLE program, define the prototype for the Java method. You'll need to use the *JAVA and *NOPASS options to handle the variable arguments.This prototype allows you to pass up to 10 arguments, which should be enough for most cases. If you need more, you can extend the prototype with additional arguments. And finally you can call the formatString method from your RPGLE program like below