Daniel Peñalba
Daniel Peñalba

Reputation: 31857

string.format equivalence in java

What is the equivalent Java implementation for string.format?

string s = string.Format("{0} - ({1})", myparam1, myparam2);

Upvotes: 2

Views: 2518

Answers (4)

Fables Alive
Fables Alive

Reputation: 2808

in Java, String.format patterns are different like %s or %1$d etc.
if you are looking for dotnet similarity it is Messageformat.format

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Accepted answer is correct. I would just like to add the Java equivalent of argument index like OP has used in his post. Please, note that I have reversed the argument positions but they would still print in order.

String fmtStr = String.format("%2$s - (%1$s)", myparam2, myparam1);

Format-string argument indices start from 1 in Java.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

Err... String.format seems like a pretty good candidate. Who would have thought? :-)

There is also MessageFormat.format.

Upvotes: 3

Andreas Dolk
Andreas Dolk

Reputation: 114767

String formatted = String.format("%s - (%s)", myparam1, myparam2);

Upvotes: 8

Related Questions