Skip to content
Advertisement

MessageFormat not formatting when single quotes are included

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}

Advertisement

Answer

The single quote must be escaped using a double single quote:

String sss = "{0}SomeText{1}''.{2}S..."
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement