[SalesForce] String.format escape curly braces

Is there a way to escape curly braces within String.format?

E.g. if I want to get the following output:

{777}

I'd expected {{ would do the job but this code throws an exception:

String.format('{{{0}}}', new String[] { String.valueOf(777) });

can't parse argument number: {{0}}

Thanks in advance!

Best Answer

This is how this is done

System.debug(LoggingLevel.INFO,String.format('\'{\'{0}\'}\'', 
     new String[] { String.valueOf(777) }));

produces {777}

SFDC String.format (also used by VF apex:outputtext) uses the Java MessageFormat class. And braces are escaped by enclosing in single quotes, which in apex must also be escaped by backslash.

See https://stackoverflow.com/questions/1187093/can-i-escape-braces-in-a-java-messageformat