String.format() 构建字符串模板

一、格式化字符串

以%字符开始的每一个格式说明符( format specifier)都会被相应的参数替代。转换字符(conversion character) 说明了要被格式化的值所属的类型:f是浮点数字,s是字符串,d是十进制整数。

  • 格式化输出的转换字符
转换字符用途Example
d十进制整数159
x或X十六进制整数9f或9F
o八进制整数237
f定点浮点数15.9
e或E指数型浮点数1.59e+01或1.59E+01
g或G普通浮点数:较短的 e/E 和 f/F——
a或A十六进制的浮点数0x1.fccdp3或0X1.FCCDP3
s或S字符串java或JAVA
c或C字符j或J
b或B布尔值false或FALSE
h或H哈希值42628b2或42628B2
t或T日期和时间过时了 很少使用
%百分比符号%
n相关分隔符——
String year = String.valueOf(LocalDateTime.now().getYear());
        int age = LocalDateTime.now().getYear() - 1994;
        System.out.println(String.format("Hello %s , happy new Year and author will be %d.",year,age));

可以指定标记符控制格式化输出的显示形式。常见的标记符

  • 格式化输出的标记符
标记用途例子
+为正数和负数打印符号+3333.33
空格在正数前加一个空格_3333.33
-左对齐3333.33_ _ _
0开头加上0003333.33
(将负值放在圆括号中(3333.33)
,进行分组3,333.33
# (用在f格式)总是包含十进制的点3333.
# (用在x或o格式)添加0x或0前缀0xcafe
$指定要格式化的参数的索引。例如:%1$ d % 1$x 以十进制和十六进制 第一个参数159 9f
<格式化前面的标记符相同值。例如:%d %d<x 以十进制和十六进制打印同样的值159 9f

二、使用 String.format() 构建短信模板

1. 定义常量类

package com.test.constants;

/**
 * 用户常量
 */
public interface UserConstants {
    String SMS_TITLE = "注册成功";
    String SMS_CONTENT = "尊敬的%s,您于%tF %tR注册成为%s平台会员,如您有什么疑问请联系我们!";
}

2. 拼接内容

package com.test;

import java.util.Date;

public class Test{
    public static void main(String[] args){
        String username = "Tom";
        String platform = "南京**创新创业平台";
        Date time = new Date();
        String newStr = String.format(UserConstants.SMS_CONTENT, username, time,time,platform);
        System.out.println(newStr);
	}
}

3. 输出结果

在这里插入图片描述

三、format 源码

​ java.lang 包

 /**
     * 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 包

/**
     * Writes a formatted string to this object's destination using the
     * specified format string and arguments.  The locale used is the one
     * defined during the construction of this formatter.
     *
     * @param  format
     *         A format string as described in <a href="#syntax">Format string
     *         syntax</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 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>.
     *
     * @throws  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="#detail">Details</a>
     *          section of the formatter class specification.
     *
     * @throws  FormatterClosedException
     *          If this formatter has been closed by invoking its {@link
     *          #close()} method
     *
     * @return  This formatter
     */
    public Formatter format(String format, Object ... args) {
        return format(l, format, args);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值