当前位置:网站首页>关于String.format(String format, Object... args)
关于String.format(String format, Object... args)
2022-06-10 21:47:00 【Li_XiaoJin】
最近在以前系统的代码上看到这个方法,觉得还不错,记录一下。
这个主要有两个用法:
- String.format(String format, Object... args) 使用指定的格式字符串和参数返回一个格式化字符串。(默认使用本地语言)
- String.format(Locale l, String format, Object... args) 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。 (使用 Locale 可以指定语言。)
通过查看源码可以看到这个方法是从 JDK 1.5 开始引入的,底层是调用了 java.util.Formatter 类的 format 方法。
/**
* 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();
}
常规类型、字符类型和数值类型的格式说明符的语法如下:%[argument_index$][flags][width][.precision]conversion
可选的 argument_index 是一个十进制整数,用于表明参数在参数列表中的位置。第一个参数由 "1$" 引用,第二个参数由 "2$" 引用,依此类推。
可选 flags 是修改输出格式的字符集。有效标志集取决于转换类型。
可选 width 是一个非负十进制整数,表明要向输出中写入的最少字符数。
可选 precision 是一个非负十进制整数,通常用来限制字符数。特定行为取决于转换类型。
所需 conversion 是一个表明应该如何格式化参数的字符。给定参数的有效转换集取决于参数的数据类型。
- s:字符串
- o:八进制数字
- x:十六进制数字
- d:十进制数字
用来表示日期和时间类型的格式说明符的语法如下:%[argument_index$][flags][width]conversion
可选的 argument_index、flags 和 width 的定义同上。
所需的 conversion 是一个由两字符组成的序列。第一个字符是 't' 或 'T'。第二个字符表明所使用的格式。这些字符类似于但不完全等同于那些由 GNU date 和 POSIX strftime(3c) 定义的字符。
与参数不对应的格式说明符的语法如下:%[flags][width]conversion
可选 flags 和 width 的定义同上。
所需的 conversion 是一个表明要在输出中所插内容的字符。
更多用法可以查看JDK文档地址:https://tool.oschina.net/apidocs/apidoc?api=jdk-zh后续使用到会陆续更新。
下面举一些例子来说明:
String.format("My name is %s%s", "li", "xj") ---> My name is lixj
String.format("%1$s,%2$s", "打工人", "打工魂")) --> 打工人,打工魂
String.format("%2$s,%1$s", "打工人", "打工魂") --> 打工魂,打工人
String.format("%o", 12)) --> 14 (10进制转8进制)
String.format("%x", 12)) --> c (10进制转16进制)
String.format("%1$,d", 5645645) --> 5,645,645 (flag 的用法,这里用都好隔开,并转换成10进制。)
String.format("%1$08d", 21729) --> 00021729
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/about-stringformat
边栏推荐
- How about opening an account with flush? Is it safe to open an account?
- Blue Bridge Cup_ Pick substring_ Combinatorial mathematics_ Multiplication principle/ Ruler method
- Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事
- Swin_ Interpretation of transformer source code
- CCF CSP 202109-4 collect cards
- Tcapulusdb Jun · industry news collection (V)
- Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
- Web3生态去中心化金融平台——Sealem Finance
- But it shows that you are the most native zaifkmw in five counties
- SQLSERVER database application and development: Chapter 9 computer operation
猜你喜欢

Web3 ecological decentralized financial platform sealem Finance

Multus CNI deployment and basic use of kubernetes multi network card scheme
![[original] analysis of nine price HPV data capture of Yilu app](/img/f2/c792367beea0956fe69aad5d35581a.png)
[original] analysis of nine price HPV data capture of Yilu app

Opencv_100问_第二章 (6-10)

Niuke.com: sum of two numbers

Question bank and simulation test of 2022 tea artist (intermediate) operation certificate examination

Swin_ Interpretation of transformer source code

Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事

How Photoshop opens, edits and exports pictures in webp format

Simulated 100 questions and simulated examination of G2 utility boiler stoker examination in 2022
随机推荐
LuoYongHao: if I were the person in charge, I could make apple products go up more than three steps
I made a blind date software source code, and I can chat with those who are interested
smb匿名
Pki/tls Swiss Army knife cfssl
Fallback operation in SVN
Software project management 6.10 Cost budget
Tcapulusdb Jun · industry news collection (IV)
Online questionnaire system based on php+web+mysql
【原创】医鹿APP九价HPV数据抓包分析
vulnhub之dc4
Whale conference empowers intelligent epidemic prevention
[content co creation] issue 17: summer is hot and you are so sweet! Participating in the signing of Huawei cloud Xiaobian, there is always a pattern gift bag that moves you!
Image mosaic camera mosaic notes
Opencv_ 100 questions_ Chapter II (6-10)
Tcapulusdb Jun · industry news collection (III)
ICML2022 | 从零开始重新审视端到端的语音到文本翻译
Reading notes on the way of Huawei data
1.Tornado简介&&本专栏搭建tornado项目简介
vulnhub之DC2
CCF CSP 202109-2 non zero segment division [100 points]