这个格式字符串有什么问题? [英] What's wrong with this format string?

查看:379
本文介绍了这个格式字符串有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < string name =q_titleformatted =false> Item %d的%d< /字符串> 

我在String.format中使用它:

  String log = String.format(getString(R.string.q_title),100,500); 

到目前为止,我发现输出没有问题。



然而,Android Studio中的代码检查给我:
$ b


格式字符串q_title不是有效的格式字符串它
不应该传递给String.format


为什么?


 <字符串名称=q_title已格式化为>解决方案

=false>%2 $ d的项目%1 $ d< / string>

和代码

  String log = getString(R.string.q_title,100,500); 

当您有多个参数时,您需要用1 $,2 $ ... n $ 。在阿拉伯语顺序是颠倒的,所以他们需要知道如何正确地改变它。
$ b

getString(id,args ...)执行格式本身。


I have a string like this:

<string name="q_title" formatted="false">Item %d of %d</string>

I'm using it in String.format like this:

String log = String.format(getString(R.string.q_title), 100, 500);

So far I've observed no problems with the output.

However, code inspection in Android Studio gives me:

Format string 'q_title' is not a valid format string so it should not be passed to String.format

Why?

解决方案

Your string should be

<string name="q_title" formatted="false">Item %1$d of %2$d</string>

And code

String log = getString(R.string.q_title, 100, 500);

When you have multiple arguments you need to mark them with 1$, 2$... n$. In arabian langs order is reversed, so they need to know how to change it correctly.

getString(id, args...) perform format in itself.

这篇关于这个格式字符串有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆