gpt4 book ai didi

android - 字符串资源文件中的格式语句

转载 作者:IT老高 更新时间:2023-10-28 13:00:37 25 4
gpt4 key购买 nike

我在通常的 strings.xml 资源文件中定义了字符串,如下所示:

<string name="hello_world"> HELLO</string>

是否可以定义如下格式字符串

 result_str = String.format("Amount: %.2f  for %d days ",  var1, var2);

在strings.xml 资源文件中?

我尝试转义特殊字符,但它不起作用。

最佳答案

您不需要在 XML 中使用 formatted="false"。您只需要使用完全限定的字符串格式标记 - %[POSITION]$[TYPE](其中 [POSITION] 是属性位置,[TYPE] 是变量类型),而不是短版本,例如 %s%d

引自 Android Docs: String Formatting and Styling :

<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 integer. You can format the string with arguments from your application like this:

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

关于android - 字符串资源文件中的格式语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12627457/

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