VIBHOR GOYAL
VIBHOR GOYAL

Reputation: 503

Message format on giving desired output

I am trying to replace the {0} in a String with a value. The code I have written for it is:

String formattedText = MessageFormat.format("'{0}'", "1");
System.out.println(formattedText);

It is giving the output as : {0}

Please let me know what I am doing wrong.

Upvotes: 0

Views: 516

Answers (4)

Vallabha Vamaravelli
Vallabha Vamaravelli

Reputation: 1293

Include double single quote should work:

MessageFormat.format("''{0}''", "1");

Or

You can use String.format:

String.format("'%s'", "1");

Upvotes: 0

Prog_G
Prog_G

Reputation: 1615

Try the below code :

 String formattedText = MessageFormat.format("''{0}''", "1");
        System.out.println(formattedText);

You can check this answer for more information.

Upvotes: 2

Kris
Kris

Reputation: 8868

It must be like

String formattedText = MessageFormat.format("{0}", "'1'");

Other wise '{}' is not considered to be a format place-holder.

Upvotes: 0

mwarren
mwarren

Reputation: 759

You need to remove the single quotes from around the {0}. The format method sees that as a literal string.

Upvotes: 0

Related Questions