java 这个格式字符串有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17502824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:11:45  来源:igfitidea点击:

What's wrong with this format string?

javaandroidstringformat

提问by Alexander Kulyakhtin

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.format 中使用它,如下所示:

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:

但是,Android Studio 中的代码检查给了我:

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

格式字符串 'q_title' 不是有效的格式字符串,因此不应将其传递给 String.format

Why?

为什么?

回答by Roger Alien

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.

当您有多个参数时,您需要用 1$、2$...n$ 标记它们。在阿拉伯语中,语言的顺序是相反的,因此他们需要知道如何正确更改它。

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

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

回答by Lazy Ninja

For percent, the following worked for me.

对于百分比,以下对我有用。

<string name="score_percent">%s%%</string>


getString(R.string.score_percent,"20")

If you are dealing with integers replace s by d

如果您正在处理整数,请用 d 替换 s

<string name="score_percent">%d%%</string>

回答by ender

For those still looking for this answer, as the link that Blackbelt postedimplies, the correct format for the string would be:

对于那些仍在寻找这个答案的人,正如Blackbelt 发布链接所暗示的那样,字符串的正确格式是:

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

回答by Garytech

Beware to escape all special characters

注意转义所有特殊字符

I had a problem with this string because I forgot to escape the percentage character " % " at the end .

我对这个字符串有问题,因为我忘记在末尾转义百分比字符“%”。

<string name="market_variation_formatter">%s %</string> 

The good escaped string was :

好的转义字符串是:

<string name="market_variation_formatter">%s \%</string>

回答by younes

If you need to format your strings, then you can do so by putting your format arguments in the string resource, as demonstrated by the following example resource.

如果您需要格式化字符串,则可以通过将格式参数放入字符串资源中来实现,如以下示例资源所示。

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. Then, format the string by calling getString(int, Object...). For example:

在这个例子中,格式字符串有两个参数:%1$s 是一个字符串,%2$d 是一个十进制数。然后,通过调用 getString(int, Object...) 格式化字符串。例如:

String text = getString(R.string.welcome_messages, username, mailCount);