在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")
$
是string templates的特殊kotlin元字符。您可以使用字符串模板机制本身来插入文字$
:
${'$'}