Format a message using MessageFormat.format() in Java

java, resourcebundle

Solution

Add an extra apostrophe `'` to the `MessageFormat` pattern `String` to ensure the `'` character is displayed

String text = 
     java.text.MessageFormat.format("You''re about to delete {0} rows.", 5);
                                         ^

An apostrophe (aka single quote) in a MessageFormat pattern starts a quoted string and is not interpreted on its own. From the javadoc

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

The `String` `You\\'re` is equivalent to adding a backslash character to the `String` so the only difference will be that `You\re` will be produced rather than `Youre`. (before double quote solution `''` applied)

Problem

I have stored some messages in a resource bundle. I'm trying to format these messages as follows. ``` import java.text.MessageFormat; String text = MessageFormat.format("You're about to delete {0} rows.", 5); System.out.println(text); ``` Assume that the first parameter i.e the actual message is stored in a property file which is somehow retrieved. The second parameter i.e 5 is a dynamic value and should be placed in the placeholder `{0}` which doesn't happen. The next line prints, Youre about to delete {0} rows. The placeholder is not replaced with the actual parameter. It is the apostrophe here - `You're`. I have tried to escape it as usual like `You\\'re` though it didn't work. What changes are needed to make it work?

Original source