正确的格式字符串String.format或类似 [英] Correct format string for String.format or similar

查看:175
本文介绍了正确的格式字符串String.format或类似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我曾经见过 String.format 这样的用法:

I'm sure I've seen String.format used like this before:

String.format("Some {1}, {2}, {3}", var1, var2, var3);

这是否会为任何人响铃?也许我在想C#,有没有办法在java中实现相同的功能?

Does this ring any bells for anyone? Maybe I'm thinking of C#, is there a way of achieving the same in java?

我知道你可以这样做:

String.format("Some %s, %d, %s", aString, aNumber, aString)

但我之后的语法是针对前者...

but the syntax I'm after is for the former...

推荐答案

您正在寻找的是 MessageFormat ,它使用给定的格式和输入参数,例如

What you are looking for is MessageFormat, which uses a given format and input parameters, e.g.

MessageFormat.format("Some {0}, {1}, {2}", var1, var2, var3);

如前所述, String.format 仍然可以使用替代语法来完成这项工作,但功能不是很强大,而不是你所要求的。

And as already mentioned, String.format can still do the job using the alternate syntax, but it is less powerful in functionality and not what you requested.

这篇关于正确的格式字符串String.format或类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆