java百分比符号,strings.xml中的百分比符号

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值