Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I escape braces in a java MessageFormat?

I want to output some braces in a java MessageFormat. For example I know the following does not work:

MessageFormat.format("  public {0} get{1}() {return {2};}\n\n", type, upperCamel, lowerCamel); 

Is there a way of escaping the braces surrounding "return {2}"?

like image 705
Steve Bosman Avatar asked Jul 27 '09 08:07

Steve Bosman


People also ask

How do you escape braces in Java?

You can use the \Q and \E special characters... anything between \Q and \E is automatically escaped.

How do you escape curly braces in string format Java?

To escape curly braces and interpolate a string inside the String. format() method use triple curly braces {{{ }}} . Similarly, you can also use the c# string interpolation feature instead of String.


2 Answers

You can put them inside single quotes e.g.

'{'return {2};'}' 

See here for more details.

like image 160
Brian Agnew Avatar answered Sep 22 '22 15:09

Brian Agnew


Wow. Surprise! The documentation for MessageFormat knows the answer:

Within a String, "''" represents a single quote. A QuotedString can contain arbitrary characters except single quotes; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".

like image 37
Bombe Avatar answered Sep 24 '22 15:09

Bombe