CoXier
CoXier

Reputation: 2663

Why I cannot format string in Java/Kotlin

Here is a string template read from file.

Name: %s
Age: %d

After I read it from file, I want to format this string using given name and age.

var template = File("file_path").readText()

MessageFormat.format(template, "Bod", 123)

print(template)

However I cannot format template. Its output is.

Name: %s
Age: %d

Upvotes: 0

Views: 282

Answers (1)

ice1000
ice1000

Reputation: 6579

You should either use String.format instead of MessageFormat.format or use {0}/{1} instead of %s/%d.

Upvotes: 2

Related Questions