Java 中的 String.Format (.NET) 等效项?
.NET(也许只是VB.NET)中的String.Format将{0},{1},...转换为确定的字符串,例如:
Dim St As String = "Test: {0}, {1}"
Console.WriteLine(String.Format(St, "Text1", "Text2"))
我尝试在Google和StackOverflows中搜索,但它们都返回数字- 字符串格式。
The String.Format in .NET (maybe just VB.NET) convert {0}, {1}, ... into determined String, for example:
Dim St As String = "Test: {0}, {1}"
Console.WriteLine(String.Format(St, "Text1", "Text2"))
I've tried to search in both Google and StackOverflows, but they all return number-string format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其他建议当然很好,但更多的是
printf< 的风格/code>
及其谱系是 Java 的最新添加内容。您发布的代码似乎受到
MessageFormat 的启发
。我不太确定
'Return:
语句正在做什么。The other suggestions are certainly good, but are more in the style of
printf
and its lineage which are more recent additions to Java. The code you posted looks to be inspired byMessageFormat
.I'm not really certain about what the
'Return:
statement is doing though.使用
MessageFormat.format
,您还可以在替换标记中提供格式化参数。或者,可以使用
String.format
,但这不能保证位置,例如String.format(“如果将 %d 乘以 %s,会得到什么?”, varNumber, varText) ;
。Use
MessageFormat.format
, you can also provide formatting arguments in the replacement tokens.Alternatively,
String.format
can be used but this doesn't guarantee position e.g.String.format("What do you get if you multiply %d by %s?", varNumber, varText);
.对于其他方法,请使用:
For other methods use: