gpt4 book ai didi

android - 格式化字符串参数计数错误

转载 作者:太空狗 更新时间:2023-10-29 16:01:02 24 4
gpt4 key购买 nike

我的 string.xml 中有一个字符串:

<string name="date_time_short">%2$d.%2$d. %1$s %2$d:%2$d</string>

现在我想通过代码设置值:

String.format(context.getResources().getString(R.string.date_time_short), day, month, at, hour, minute);

但是我得到了错误:

Wrong argument count, format string date_time_short requires 2 but format call supplies 5

因此,代表字符串的 %1$s 似乎有问题。至少这是写在文档中的 here

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following 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. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

那么为什么我会收到此错误?

最佳答案

例如 %1$s 中的数字表示您的 Object... 参数中的索引。所以在你的例子中,字符串的正确定义是:

<string name="date_time_short">%1$d.%2$d. %3$s %4$d:%5$d</string>

关于android - 格式化字符串参数计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com