Reputation: 151
In order to format a string, I always use String.format(getString(R.string.something),arg1)
However I saw on the documentation getString(string, args)
could do the same.
Therefore, I wonder why everyone use String.format if getString can do the same
Thank you
Upvotes: 0
Views: 2738
Reputation: 494
You are mistaken, there is no method Resources.getString(string, args...)
or Context.getString(string, args...)
. There is Resources.getString(int, args...)
that takes an int and objects.
When formatting a string you would use String.format(string, args...)
. But in Android your string resources are probably in the res folder so you have a convinience method for getting the string from the resources and formatting it with String.format
. It does the same thing as you did (taken from Android source):
@NonNull
public String getString(@StringRes int id, Object... formatArgs) throws NotFoundException {
final String raw = getString(id);
return String.format(mResourcesImpl.getConfiguration().getLocales().get(0), raw,
formatArgs);
}
You would use it like:
getString(R.string.something, arg1)
and save yourself some typing.
Upvotes: 2