Escape single quotes when using Java MessageFormat

If you are using Java MessageFormat, you will possibly encounter the issue that single quotes are not rendered.

/* string used in code */
"Hello, it's me!"

/* output */
"Hello, its me!"

So, how to escape single quotes?

It's simple! According to the API:

A single quote itself must be represented by doubled single quotes '' throughout a String.

/* string used in code */
"Hello, it'''s me!"

/* output */
"Hello, it's me!"

What is the purpose of single quotes in MessageFormat?

Well, you can use a pair of single quotes, within a String, to quote any arbitrary characters (except single quotes). For example, pattern string "'{0}'" represents string "{0}", not a FormatElement.


SHARE THIS ARTICLE