String.format(String format, Object... args)方法详解

很多次见到同事使用这个方法,同时看到https://blog.csdn.net/qq_27298687/article/details/68921934这位仁兄写的非常仔细,我也记录一下,好加深印象。

这个是从java5的时候添加进去的方法。

/**
* Returns a formatted string using the specified format string and
* arguments.
*
* <p> The locale always used is the one returned by {@link
* java.util.Locale#getDefault() Locale.getDefault()}.
*
* @param format
* A <a href="../util/Formatter.html#syntax">format string</a>
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java&trade; Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the <a
* href="../util/Formatter.html#syntax">conversion</a>.
*
* @throws java.util.IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section of the
* formatter class specification.
*
* @return A formatted string
*
* @see java.util.Formatter
* @since 1.5
*/
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
如果往里点进去是掉用了java.util.Formatter类的format方法。
这个类里有详细的说明文档:
  • 常规类型、字符类型和数值类型的格式说明符的语法如下:
       %[argument_index$][flags][width][.precision]conversion
     

    可选的 argument_index 是一个十进制整数,用于表明参数在参数列表中的位置。第一个参数由 "1$" 引用,第二个参数由 "2$" 引用,依此类推。

    可选 flags 是修改输出格式的字符集。有效标志集取决于转换类型。

    可选 width 是一个非负十进制整数,表明要向输出中写入的最少字符数。

    可选 precision 是一个非负十进制整数,通常用来限制字符数。特定行为取决于转换类型。

    所需 conversion 是一个表明应该如何格式化参数的字符。给定参数的有效转换集取决于参数的数据类型。

  s:字符串

  o:八进制数字

  x:十六进制数字

  d:十进制数字    

  • 用来表示日期和时间类型的格式说明符的语法如下:
       %[argument_index$][flags][width]conversion
     

    可选的 argument_indexflags 和 width 的定义同上。

    所需的 conversion 是一个由两字符组成的序列。第一个字符是 't' 或 'T'。第二个字符表明所使用的格式。这些字符类似于但不完全等同于那些由 GNU date 和 POSIX strftime(3c) 定义的字符。

  • 与参数不对应的格式说明符的语法如下:
       %[flags][width]conversion
     

    可选 flags 和 width 的定义同上。

    所需的 conversion 是一个表明要在输出中所插内容的字符。

我在这里就不一一赘述了,感兴趣的同学可以参考jdk文档 http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

我仅仅举几个例子稍做说明:

String.format("你可以成为%s","平凡的人")  ------>   你可以成为平凡的人(字符串替换)

String.format("你可以成为%s,他也可以成为%s","平凡的人","不平凡的人")  ------>   你可以成为平凡的人,他也可以成为不平凡的人。

String.format("你可以成为%2$s,他也可以成为%1$s","平凡的人","不平凡的人")  ------>   你可以成为不平凡的人,他也可以成为平凡的人。(位置控制通过[argument_index$])

String.format("%o", 12)); ---------->14  (10进制转8进制)

String.format("%x", 12)); ----------> c  (10进制转16进制)

String.format("%1$,d", 12302562); -------------> 12,302,562  (flag 的用法,这里用都好隔开,并转换成10进制。)

String.format("%1$08d", 123456);--------------> 00123456  (width的用法,用0填充(flag),最少8位。

好困,下次接着写吧。睡觉了。

猜你喜欢

转载自www.cnblogs.com/bfyq/p/9256390.html