一、格式化字符串
以%字符开始的每一个格式说明符( 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 | 开头加上0 | 003333.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™ 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™ 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);
}