这是从文件中读取的字符串模板
。
Name: %s
Age: %d
从文件中读取它后,我想使用给定的名称和年龄来格式化该字符串。
var template = File("file_path").readText()
MessageFormat.format(template, "Bod", 123)
print(template)
但是我无法格式化模板
。它的输出是。
Name: %s
Age: %d
最佳答案
您应该使用 String.format
而不是 MessageFormat.format
或使用 {0}
/{1}
而不是 %s
/%d
。
关于java - 为什么我无法在 Java/Kotlin 中格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062028/