Format a message using MessageFormat.format() in Java
Asked Answered
I

6

97

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?

Ironing answered 10/7, 2013 at 11:34 Comment(0)
E
158

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)

Emrick answered 10/7, 2013 at 11:36 Comment(2)
Is there any other method to do format like this but not converting single quote to double single quote? because it is annoying.Exegete
Why in C # I do not need to do this?Ivyiwis
H
14

Just be sure you have used double apostrophe ('')

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

Edit:

Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. ...

Any unmatched quote is treated as closed at the end of the given pattern. For example, pattern string "'{0}" is treated as pattern "'{0}'".

Source http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html

Housework answered 10/7, 2013 at 12:2 Comment(0)
H
6

You need to use double apostrophe instead of single in the "You''re", eg:

String text = java.text.MessageFormat.format("You''re about to delete {0} rows.", 5);
System.out.println(text);
Handcrafted answered 10/7, 2013 at 11:38 Comment(0)
M
5

Using an apostrophe (Unicode: \u2019) instead of a single quote ' fixed the issue without doubling the \'.

Minimal answered 14/12, 2017 at 16:47 Comment(0)
P
3

For everyone that has Android problems in the string.xml, use \'\' instead of single quote.

Pairs answered 30/7, 2014 at 9:49 Comment(0)
C
0

Here is a method that does not require editing the code and works regardless of the number of characters.

String text = 
  java.text.MessageFormat.format(
    "You're about to delete {0} rows.".replaceAll("'", "''"), 5);
Carlist answered 16/10, 2020 at 5:39 Comment(1)
Why replaceAll if it's not a regex? Can only use replaceWoden

© 2022 - 2024 — McMap. All rights reserved.