当前位置:网站首页>About string format(String format, Object... args)

About string format(String format, Object... args)

2022-06-10 23:10:00 Li_ XiaoJin

I recently saw this method in the code of the previous system , I think it's not bad , Make a note of .

This has two main uses :

  1. String.format(String format, Object... args) Returns a format string with the specified format string and parameters .( Default to local language )
  2. String.format(Locale l, String format, Object... args) Use the specified language environment 、 Format string and parameters return a format string . ( Use Locale You can specify the language .)

From the source code, you can see that this method is from JDK 1.5 Introduced , At the bottom is the call java.util.Formatter Class format Method .

    /**
     * Returns a formatted string using the specified format string and
     * arguments.
     *
     * @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();
    }


    public static String format(Locale l, String format, Object... args) {
        return new Formatter(l).format(format, args).toString();
    }

General types 、 The syntax of the format specifiers for character types and numeric types is as follows :%[argument_index$][flags][width][.precision]conversion

Optional argument_index It's a decimal integer , Used to indicate the position of the parameter in the parameter list . The first parameter consists of "1$" quote , The second parameter consists of "2$" quote , And so on .

Optional flags Is to modify the character set of the output format . The set of valid flags depends on the conversion type .

Optional width Is a nonnegative decimal integer , Indicates the minimum number of characters to write to the output .

Optional precision Is a nonnegative decimal integer , Usually used to limit the number of characters . The specific behavior depends on the type of transformation .

what is needed conversion Is a character that indicates how the parameter should be formatted . The valid conversion set for a given parameter depends on the data type of the parameter .

  • s: character string
  • o: Octal numbers
  • x: Hexadecimal number
  • d: Decimal number

The syntax for the format specifier used to represent date and time types is as follows :%[argument_index$][flags][width]conversion

Optional argument_index、flags and width As defined above .

The required conversion It's a two character sequence . The first character is 't' or 'T'. The second character indicates the format used . These characters are similar to but not exactly equivalent to those by GNU date and POSIX strftime(3c) Defined characters .

The syntax of a format specifier that does not correspond to a parameter is as follows :%[flags][width]conversion

Optional flags and width As defined above .

The required conversion Is a character indicating what to insert in the output .

More usage can be found in JDK Document address :https://tool.oschina.net/apidocs/apidoc?api=jdk-zh Subsequent use will be updated in succession .

Here are some examples to illustrate :

String.format("My name is %s%s", "li", "xj")    ---> My name is lixj

String.format("%1$s,%2$s", " Workers ", " Working soul "))        -->  Workers , Working soul 
String.format("%2$s,%1$s", " Workers ", " Working soul ")        -->  Working soul , Workers 

String.format("%o", 12))        --> 14  (10 Turn into the system 8 Base number )

String.format("%x", 12))        --> c  (10 Turn into the system 16 Base number )

String.format("%1$,d", 5645645)        --> 5,645,645 (flag  Usage of , It's easy to separate here , Translates into 10 Base number .)

String.format("%1$08d", 21729)        --> 00021729

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/about-stringformat

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102147101070.html