Alex Rivera | Logout

Java Equivalent to .NET's String.Format

Asked 2010-09-20T19:03:46.160
63

Is there an equivalent to .NET's String.Format in Java?

Edit
Report

1 Answer

21

You can also simply use %s for string, since the index is an optional argument.

String name = "Jon";
int age = 26;
String.format("%s is %s years old.", name, age);

The above example looks cleaner in my opinion.

A note about %s from the java documentation:

If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().

answered 2013-01-22T18:13:09.507

Your Answer