Im trying to format a string from string.xml to reuse it with several Values. I'm running into some problems as the String should also contain the percentage-symbol which is used by the formatter. I already tried to replace the % symbol by its unicode presentation but that doesnt seem to work:
The new value is %1$s%
The new value is %1$s\u0025
The new value is %1$s
Java Code:
String value = "25";
String formattedString = context.getResources().getString(R.string.teststring), value);
In this example. teststring and teststring2 will produce an error, while teststring3 works fine. Whats the correct way to put that % symbol in the xml-string to get "The new value is 25%" as formatted String?
Thanks for your help.
Stacktrace:
FATAL EXCEPTION: main
java.util.UnknownFormatConversionException: Conversion:
at java.util.Formatter$FormatSpecifierParser.unknownFormatConversionException(Formatter.java:2304)
at java.util.Formatter$FormatSpecifierParser.advance(Formatter.java:2298)
at java.util.Formatter$FormatSpecifierParser.parseConversionType(Formatter.java:2377)
at java.util.Formatter$FormatSpecifierParser.parseArgumentIndexAndFlags(Formatter.java:2348)
at java.util.Formatter$FormatSpecifierParser.parseFormatToken(Formatter.java:2281)
at java.util.Formatter.doFormat(Formatter.java:1069)
at java.util.Formatter.format(Formatter.java:1040)
at java.util.Formatter.format(Formatter.java:1009)
at java.lang.String.format(String.java:1988)
at android.content.res.Resources.getString(Resources.java:343)
at (... and so on)
--EDIT--
Thanks for the Help. The correct answer is
The new value if %1$s%%
formatted="false" must not be set.
解决方案
Use
The new value is %1$s%%
In java
String value = "25";
String formattedString =
String.format(getResources().getString(R.string.teststring), value);
Log.i("",formattedString);