kotlin,如何使用'$'作为String.format中的参数索引

问题描述 投票:0回答:1

在kotlin中,如何使用'$'作为字符串中的参数索引进行格式化。即要将相同的数字122放在字符串的第一位置和第三位置。在kotlin中,此字符串不喜欢'$'。

((注意:strFormat是动态生成的,不能存储在字符串资源文件中)。

        val strFormat = "%d, %s, same as 1st int: %1$d, same as 1st string: %1$s, $3$s"
        val str = String.format(strFormat, 122, "the string one", "string two")

enter image description here

android-studio kotlin string-formatting
1个回答
0
投票

$string templates的特殊kotlin元字符。您可以使用字符串模板机制本身来插入文字$

${'$'}
© www.soinside.com 2019 - 2024. All rights reserved.