关于String.format()的使用

在日常工作中,经常涉及到字符串的操作,记录一下String.format()的相关方法使用.

参考资料:

https://www.jianshu.com/p/359d9001ebbc

https://www.cnblogs.com/jpfss/p/12027502.html

https://blog.csdn.net/wahaha13168/article/details/83008576

1 String.format()的简介

String.format()使用类似占位符的处理, 将参数拼接到字符串中.

    /**
     * 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();
    }

String.format()方法里面调用了java.util.Formatter.format()方法.该方法有一个解析说明

%[argument_index$][flags][width][.precision]conversion

说明:

  • argument_index$ 可选, 十进制整数,表示参数在列表中的位置. 如: 1$表示第一个参数.
  • flags 可选,控制输出的格式
  • width 可选,一个正整数,表示输出的最小长度
  • precision 可选,限定输出字符数
  • conversion 必传,格式化参数的字符

案例:

 System.out.println(String.format("我学%2$s,她学%1$s", "Python","Java"));
// argument_index$  2$表示参数的第二个, 1$表示参数的第一个
// 我学java,她学Python

System.out.println(String.format("%1$08d", 1234));
// 0是flags,用来填充  8是width,表示长度为8 所以下面用0补齐位数到8位
// 00001234

System.out.println(String.format("%1$.3f", 3.1415925));
// precision  保留小数的位数   保留规则是四舍五入
// 3.142

类型符:

转换符 说明 说明
%s 字符串类型 “libai”
%c 字符类型 ‘l’
%b 布尔类型 true
%d 整数类型(十进制) 66
%x 整数类型(十六进制) EE
%o 整数类型(八进制) 66
%f 浮点类型 66.66
%a 十六进制浮点类型 EE.35AA
%e 指数类型 1.1e+4
%g 通用浮点类型(f和e类型中较短的)
%h 散列码
%% 百分比类型
%n 换行符
%tx 日期与时间类型(x代表不同的日期与时间转换符) tY表示年;tm表示月;te表示日

2 String.format()的使用

       
public static void main(String[] args) {
    
    
		// 1 字符串类型  %s
        System.out.println(String.format("你好,%s", "李白"));
        // 2 字符类型  %c   换行符 %n
        System.out.printf("第一个字母大写是:%c %n", 'A');
        // 3 布尔类型  %b   换行符 %n
        System.out.printf("30>70的结果是:%b %n", 30 > 70);
        // 4 整数类型(十进制) %d   换行符 %n
        System.out.printf("120的一半是:%d %n", 120 / 2);
        // 5 整数类型(十六进制)  %x   换行符 %n
        System.out.printf("120的16进制数是:%x %n", 120);
        // 6 整数类型(八进制) %o   换行符 %n
        System.out.printf("120的8进制数是:%o %n", 120);
        // 7 浮点类型  %f   换行符 %n
        System.out.printf("100元的书打8.5折扣是:%f 元%n", 100 * 0.85);
        // 8 十六进制浮点类型  %a   换行符 %n
        System.out.printf("上面价格的16进制数是:%a %n", 100 * 0.85);
        // 9 指数类型  %e   换行符 %n
        System.out.printf("上面价格的指数表示:%e %n", 100 * 0.85);
        // 10 通用浮点类型(f和e类型中较短的)  %g   换行符 %n
        System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 100 * 0.85);
        // 11 整数类型(十进制) %d  百分比类型 %%   换行符 %n
        System.out.printf("上面的折扣是%d%% %n", 85);
        // 12 散列码  %h   换行符 %n
        System.out.printf("字母A的散列码是:%h %n", 'A');
        // 13 日期与时间类型(x代表不同的日期与时间转换符) tY表示年;tm表示月;te表示日
        System.out.println(String.format("今天的日期为: %1$tY-%1$tm-%1$te", new Date()));
}   

/*  运行结果:

你好,李白
第一个字母大写是:A 
30>70的结果是:false 
120的一半是:60 
120的16进制数是:78 
120的8进制数是:170 
100元的书打8.5折扣是:85.000000 元
上面价格的16进制数是:0x1.54p6 
上面价格的指数表示:8.500000e+01 
上面价格的指数和浮点数结果的长度较短的是:85.0000 
上面的折扣是85% 
字母A的散列码是:41 
今天的日期为: 2021-10-29
*/

猜你喜欢

转载自blog.csdn.net/ABestRookie/article/details/121043640