Reputation: 503
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
Reputation: 1293
Include double single quote should work:
MessageFormat.format("''{0}''", "1");
Or
You can use String.format:
String.format("'%s'", "1");
Upvotes: 0
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
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
Reputation: 759
You need to remove the single quotes from around the {0}. The format method sees that as a literal string.
Upvotes: 0