Java string substitution/formatting

satya - Saturday, February 11, 2012 11:45:06 AM

Goal

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.

satya - Saturday, February 11, 2012 11:46:06 AM

I have looked at variable arguments before here

I have looked at variable arguments before here

satya - Saturday, February 11, 2012 11:46:57 AM

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

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

satya - Saturday, February 11, 2012 11:47:41 AM

satya komatineni java string format

satya komatineni java string format

Search for: satya komatineni java string format

satya - Saturday, February 11, 2012 11:48:31 AM

Java String.format

Java String.format

Search for: Java String.format

satya - Saturday, February 11, 2012 11:51:55 AM

Here is java format string specification

Here is java format string specification

satya - Saturday, February 11, 2012 11:53:13 AM

General idea


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

satya - Saturday, February 18, 2012 1:35:48 PM

You may have to qualify the %1 as %1s


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