MessageFormat.formatJDK 1.5和JDK 1.5有什么区别String.format?
2 回答
简而言之,主要区别在于格式字符串:
MessageFormat.format()格式字符串接受参数位置(例如{0},,{1})。例子:"This is year {0}!"开发人员不必担心参数类型,因为它们通常会根据当前的
Locale.String.format()格式字符串接受参数类型说明符(例如,%d对于数字,%s对于字符串)。例子:"This is year %d!"String.format()由于您可以使用类型说明符指定许多选项,因此通常可以更好地控制参数的显示方式。例如,格式字符串"%-6.2f"指定以 min 显示左对齐的浮点数。宽度为 6 个字符,精度为 2 位小数。
只需查看这两种方法的 javadoc 即可了解更多详细信息。
String.format is just a shortcut to Formatter, this is a "printf-style" formatter. On the other side, MessageFormat uses a different formatting convention, as described in the linked documentation.
Use the first "for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output" and the second "to produce concatenated messages in language-neutral way".