31

我正在尝试格式化 string.xml 中的字符串以将其与多个值一起重用。我遇到了一些问题,因为字符串还应该包含格式化程序使用的百分比符号。我已经尝试用它的 Unicode 表示来替换 % 符号,但这似乎不起作用:

<?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 代码:

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

在这个例子中,teststringteststring2产生一个错误,而teststring3工作正常。将 % 符号放在 xml 字符串中以获得“新值为 25%”作为格式化字符串的正确方法是什么?

堆栈跟踪:

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)

正确答案是

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

不得设置 formatted="false"。

4

7 回答 7

42

利用

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

</resources>

在java中

String value = "25";
String formattedString =
    String.format(getResources().getString(R.string.teststring), value);
Log.i("",formattedString);
于 2013-05-30T10:57:23.000 回答
9

您可以尝试\%%代替 % 吗?

喜欢,

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

或者

<string formatted="false" name="teststring" >The new value is %</string>
于 2013-05-30T10:55:46.777 回答
8

尝试这个

<string  name="teststring"> The new value is %1$s %% </string>
于 2013-05-30T11:12:07.200 回答
1

检查这个,

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

String value = "25";
String formattedString = this.getResources().getString(R.string.teststring, mPageNumber);
于 2013-05-30T11:07:09.183 回答
1
String value = "25";

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

如果现在 Log.d(TAG, formattedString); 它会记录

The new value is 25%
于 2015-04-13T01:29:37.970 回答
0

像这样使用:

String str = context.getResources().getString(R.string.teststring);
String formattedString = String.format(str, value);
于 2013-05-30T10:57:42.580 回答
0
<?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);
于 2018-08-02T14:08:05.057 回答