Alexander Kulyakhtin Alexander Kulyakhtin - 1 year ago 433
Java Question

What's wrong with this format string?

I have a string like this:

<string name="q_title" formatted="false">Item %d of %d</string>

I'm using it in String.format like this:

String log = String.format(getString(R.string.q_title), 100, 500);

So far I've observed no problems with the output.

However, code inspection in Android Studio gives me:

Format string 'q_title' is not a valid format string so it
should not be passed to String.format


Answer Source

Your string should be

<string name="q_title" formatted="false">Item %1$d of %2$d</string>

And code

String log = getString(R.string.q_title, 100, 500);

When you have multiple arguments you need to mark them with 1$, 2$... n$. In arabian langs order is reversed, so they need to know how to change it correctly.

getString(id, args...) perform format in itself.