Getting "Format method not found" when trying to use java.lang.string method from ColdFusion

I want to do something in ColdFusion that looks like sprintf in C or Perl. I found this answer , which seems to be what I'm looking for. However, I cannot get it to work.

Here is what I am trying:

<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>

And here is what I get:

cfcatch.Message: format method not found.

cfcatch.Detail: either there are no methods with the specified method, the names and types of arguments, or the formatting method overloaded argument types that ColdFusion cannot reliably decrypt. Cold synthesis found 0 methods that match the provided arguments. If it is a Java object, and you have confirmed that the method exists, use javacast to reduce the 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))>

.

String, valueOf, .

Edit: , , , , , . , , , . , , , , , , . , , cfloop cfoutput , . DateFormat, NumberFormat, Left, Right .. , B.

ColdFusion 9.01, Windows 7, Java 1.6.0_22.

.

+3
1

:

, , , - (String, Object []), String Object.

: format("Hello, %s!", [firstName]).

, , :)

+1

All Articles