Java string substitution/formatting

I want to log strings effectively so that the entire string is not expanded before making a decision to log the string or not.

This implies I need to postpone string concatenation until I decide I want to log or now

So I need to pass the arguments to string substitution.

I have looked at variable arguments before here

I have also looked at string formatting in the context Android Resources here

satya komatineni java string format

Search for: satya komatineni java string format

Java String.format

Search for: Java String.format

Here is java format string specification


Object o1;
String o2;
String formatstring = "Hello there %1 and such %2";
String ns = String.format(formatstring, o1, o2);

Object o1;
String o2;
String formatstring = "Hello there %1s and such %2s";
String ns = String.format(formatstring, o1, o2);