简体   繁体   中英

string.format equivalence in java

string.format的等效Java实现是什么?

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

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

There is also MessageFormat.format .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM