I am passing in text which is combination of {} filler and text. I am trying to fill {} with some values and tried using MessageFormat.
String sss = "{0}SomeText{1}\'.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}";
Object[] testArgs = {"nits1", "Nits2","nits#"};
System.out.println(MessageFormat.format(sss,testArgs));
OUTPUT
nits1SomeTextNits2.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}
The single quote must be escaped using a double single quote:
String sss = "{0}SomeText{1}''.{2}S..."
My mistake was, that I didn't use the returned value from method 'format(..)'
Wrong code:
MessageFormat.format(sss, testArgs);
System.out.println(sss);
Correct code:
String newString = MessageFormat.format(sss, testArgs);
System.out.println(newString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With