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}"?
you can use this regex with pearl or any other language to escape curly brackets and single quotes (x27). It does not touch any placeholder e.g.
{0}
:bash echo "# 'single' quote test \n\n public {0} get{1}() {return {2};}\n\n" | perl -pe 's/\x27/\x27\x27/g; s/\{([^0-9])/\x27\{\x27$1/g; s/([^0-9])\}/$1\x27\}\x27/g'
You can put them inside single quotes e.g.
See here for more details.
Use single quotes:
If you want to actually use a single quote, just double it. The JavaDoc for
MessageFormat
gives this somewhat complicated example:This is
''
for a single quote, then'{'
for an escaped brace, then0
,'}'
for the closing brace and''
for the closing quote.Wow. Surprise! The documentation for MessageFormat knows the answer: