As title says, I want to know is there any convent way to format string using array in Java.
Let me hold an kotlin example.
var sentences = arrayOf("hello", "world")
var template = "Dialogue: ${sentences[0]}\n" +
"Dialogue: ${sentences[1]}\n"
template.format(sentences)
print(template)
Above code works well. So how about Java
?
EDIT
I am sorry I have not described my question clearly. And when I run into my real case I found my code cannot work for me now.
In fact, template
is read from a file.
var sentences = arrayOf("hello", "world")
var template = File("template.txt").readText()
template.format(sentences)
print(template)
And template.txt file contains:
Dialogue: ${sentences[0]}
Dialogue: ${sentences[1]}
As you see, I want to read file then format the result.
In addition, I am so sorry about this question. Because it seems to be changed to how to format string which is read from file.