percentage symbol in strings.xml

随声附和 提交于 2019-12-21 06:49:28

问题


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:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="teststring">The new value is %1$s%</string>
    <string name="teststring2">The new value is %1$s\u0025</string>
    <string name="teststring3">The new value is %1$s</string>
</resources>

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

<string name="teststring">The new value if %1$s%%</string>

formatted="false" must not be set.


回答1:


Use

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="teststring">The new value is %1$s%%</string>

</resources>

In java

String value = "25";
String formattedString =
    String.format(getResources().getString(R.string.teststring), value);
Log.i("",formattedString);



回答2:


Can you try \%% instead of % ??

Like,

<string name="teststring">The new value is \%%</string>

Or

<string formatted="false" name="teststring" >The new value is %</string>



回答3:


Try this

<string  name="teststring"> The new value is %1$s %% </string>



回答4:


Check this one,

<string name="teststring">The new value is %1$s %%</string>

String value = "25";
String formattedString = this.getResources().getString(R.string.teststring, mPageNumber);



回答5:


String value = "25";

String formattedString = "The new value is " + value + (char) 0x0025;

If now Log.d(TAG, formattedString); it will logs

The new value is 25%



回答6:


Use like this:

String str = context.getResources().getString(R.string.teststring);
String formattedString = String.format(str, value);



回答7:


<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="offerString">The new value is %1$s%% %2$s</string>
    </resources>

String offerOnHdfc = "25";
String formattedString = String.format(getResources().getString(R.string.offerString), offerOnHdfc);
Log.i("",formattedString);


来源:https://stackoverflow.com/questions/16834196/percentage-symbol-in-strings-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!