Java中StringFormat和MessageFormat的区别

Java中StringFormat和MessageFormat的区别

MessageFormat.format()

format string accepts argument positions (eg. {0}, {1}). Example:

MessageFormat.format("This is year {0}!",1992)

The developer doesn't have to worry about argument types, because they are, most often, recognized and formated according to current Locale.

String.format()

format string accepts argument type specifiers (eg. %d for numbers, %s for strings). Example:

String.format("This is year %d!")

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.

ref

difference between MessageFormat.format and String.format in jdk1.5?

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,872评论 0 10
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,996评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,930评论 0 13
  • 昨晚孩子们回家背诵了课文,今天课堂上我们就要来检查一下。在家里背得滚瓜烂熟的孩子,一听老师要开始检查了,小...
    闪亮的欣欣阅读 187评论 0 0
  • 原题出处
    黑夜与繁星阅读 154评论 0 0

友情链接更多精彩内容