I want to do something in ColdFusion that is similar to sprintf in C or Perl. I found this answer, which seems to be what I m looking for. However, I can t get it to work.
我在此试图:
<cftry>
<cfset firstName="John">
<cfset output=createObject("java","java.lang.String").format("Hello, %s!", firstName)>
<cfcatch type="any">
<cfdump var="#cfcatch#" expand="false">
</cfcatch>
<cftry>
这里我得到的是:
副渔获物。 电文:没有找到格式方法。
cfcatch.Detail: Either there are no methods with the specified method name and argument types or the format method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.
这是一个超负荷的方法,因此,我按照建议使用 JavaCast的论点:
<cfset output=createObject("java","java.lang.String").format(JavaCast( string , "Hello, %s!"), firstName)>
<cfset output=createObject("java","java.lang.String").format("Hello, %s!", JavaCast( string , firstName))>
<cfset output=createObject("java","java.lang.String").format(JavaCast( string , "Hello, %s!"), JavaCast( string , firstName))>
并且每次都出现同样的错误。
我在Sting阶级、价值Of上尝试了另一种静态方法,并做了罚款。
Edit: I ve already seen a comment, and I m not sure how to respond to those, but maybe I should explain here. What I ve shown above is an extremely simplified example of what I am trying to do. The goal is to use a format string to provide lots of formatting in one place, and then simply pass in a list of variables, instead of formatting a bunch of variables and outputting them, or formatting them as I m outputting them. With the format method, I plan to build a set of format strings that match the output I need, then I will just cfloop or cfoutput over a query, run this one method inside, and get the output I want. No DateFormat, NumberFormat, Left, Right, etc. If I can t get this working, that is plan B though.
I m 驾 Cold9.01,Windows 7, Java 1.6.0_22。
非常赞赏一切帮助。