views:

100

answers:

2

Is there an equivalent to the .NET/C#'s String.Format in Java?

+3  A: 

There is a String.format in Java, although the syntax is a little different from in .NET.

Mark Byers
+7  A: 

Have a look at the String.format and PrintStream.format methods.

Both are based on the java.util.Formatter class.

String.format example:

Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
// -> s == "Duke's Birthday: May 23, 1995"

System.out.format example:

// Writes a formatted string to System.out.
System.out.format("Local time: %tT", Calendar.getInstance());
// -> "Local time: 13:34:18"
dtb