简体   繁体   English

java中的string.format等价

[英]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. 错误... String.format似乎是一个非常好的候选人。 Who would have thought? 谁曾想到? :-) :-)

There is also MessageFormat.format . 还有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. 我想在他的帖子中添加像OP一样的Java等价的参数索引 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. Format-string参数索引在Java中从1开始。

in Java, String.format patterns are different like %s or %1$d etc. 在Java中,String.format模式与%s或%1 $ d等不同。
if you are looking for dotnet similarity it is Messageformat.format 如果您正在寻找dotnet相似性,那么它就是Messageformat.format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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