使用 ColdFusion 重现这种类似 printf 的格式的最直接方法是什么?
我因为一项非常简单的任务而被投入 ColdFusion。该应用程序有一些显示“帮助代码”的逻辑(我们不讨论什么是帮助代码),但是,该逻辑有缺陷,需要修复。给定一个两个字母的代码、一个 1-4 位数字和另一个 1-2 位数字,我需要像这个 printf
调用一样显示它们:
printf("%s%04d%02d", letterCode, bigNumber, smallNumber);
如果您不熟悉 printf函数中,它接受一个格式字符串(第一个参数),并根据给定的格式在其中写入其他变量。 %s
表示“写入字符串”,%d
表示“写入数字”; %0zd
表示“写入一个数字并用零填充,使其长度至少为 z 个字符(因此 %04d
表示“写入一个数字并用零填充)用零填充,使其长度至少为 4 位)。
以下是 %s%04d%02d
的一些示例:
"AD", 45, 12: AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0: FO000100
但是,这是我第一次使用 ColdFusion,我找不到类似 printf
或 的内容sprintf
格式化字符串。
另一个不再在这里工作的人诉诸了(非工作)循环,我认为最好使用库代码而不是实际修复循环,因为无论如何我可能需要再次做类似的事情。
I've been thrown into ColdFusion for a very simple assignment. The application has some logic to display "help codes" (let's not get into what is a help code), however, the logic is buggy and needs to be fixed. Given a two-letters code, a 1-4 digits number, and another 1-2 digits number, I would need to display them like this printf
call would:
printf("%s%04d%02d", letterCode, bigNumber, smallNumber);
If you're not familiar with the printf function, it accepts a format string (the first parameter), and writes the other variables in it according to the given format. %s
means "write a string" and %d
means "write a number"; %0zd
means "write a number and pad it with zeroes so it's at least z characters long (so %04d
means "write a number and pad it with zeroes so it lengths at least 4 digits).
Here are a few examples with %s%04d%02d
:
"AD", 45, 12: AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0: FO000100
However, it's my very first time with ColdFusion, and I couldn't find anything like printf
or sprintf
to format strings.
The other guy, who doesn't work here anymore, resorted to a (non-working) loop, and I thought it would be better to use library code instead of actually fixing the loop, since anyways I might need to do similar things again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者...按照 bpanulla 的建议,并由 Leigh 纠正
Or alternatively... as suggested by bpanulla, and corrected by Leigh
您可以使用
NumberFormat
在 CF 中用前导零填充数字。You can use
NumberFormat
to pad a number with leading zeros in CF.在支持 ColdFusion 的 Java 层中有很多方法可以做到这一点。以下是一个 Java 资源:
http://download。 oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
使用 CFOBJECT 或 CreateObject 创建所需的 Java 类的实例。
There's lots of ways to do this in the Java layer underpinning ColdFusion. Here's one Java resource:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
Make instances of the Java classes you need with CFOBJECT or CreateObject.
我假设您正在将这些显示到网页上?如果是这样,我会使用 switch/case 语句。既然你说“给定两个字母的代码......”,开关/外壳就可以很好地工作。例如:
或者您可以使用 if/else 代替。但要点(回答你的问题)是在 ColdFusion 中你只需键入显示字符(无论是帮助代码、文本还是其他)。您不需要使用特殊函数来向页面显示文本。
I am assuming you are displaying these to a webpage? If so, I would use a switch/case statement. Since you said "given a two-letter code...", a switch/case would work well. For example:
Or you could use an if/else instead. But the main point (to answer your question) is that in ColdFusion you just type out the display characters (be it help codes, or text, or whatever). You don't need to use a special function to display text to the page.