JDK 1.5中的MessageFormat.format和String.format有什么区别? [英] What is the difference between MessageFormat.format and String.format in JDK 1.5?
问题描述
JDK 1.5中的 MessageFormat.format
和 String.format
有什么区别?
What is the difference between MessageFormat.format
and String.format
in JDK 1.5?
推荐答案
简单地说,主要区别在于格式字符串:
Put simply, the main difference is in format string:
-
MessageFormat.format()
格式字符串接受参数位置(例如{0}
,{1}
).示例:
MessageFormat.format()
format string accepts argument positions (eg.{0}
,{1}
). Example:
今年{0}!"
开发人员不必担心参数类型,因为它们通常根据当前的 Locale
进行识别和格式化.
The developer doesn't have to worry about argument types, because they are, most often, recognized and formated according to current Locale
.
String.format()
格式字符串接受参数类型说明符(例如,%d
用于数字,%s
用于字符串).示例:
String.format()
format string accepts argument type specifiers (eg. %d
for numbers, %s
for strings). Example:
这是%d年!"
String.format()
通常可让您更好地控制参数的显示方式.例如,格式字符串%-6.2f"
指定显示带有最小值的左对齐浮点数.宽度为6个字符,精度为2个小数位.
String.format()
generally gives you much more control over how the argument is displayed thanks to many options you can specify with the type specifier. For instance, format string "%-6.2f"
specifies to display a left-aligned floating point number with min. width 6 chars and precision of 2 decimal places.
只需看看这两种方法的javadoc即可了解更多详细信息.
Just have a look at javadoc of both methods to find out more details.
这篇关于JDK 1.5中的MessageFormat.format和String.format有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!