当前位置:网站首页>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 :
- String.format(String format, Object... args) Returns a format string with the specified format string and parameters .( Default to local language )
- 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
边栏推荐
- 简单阻抗匹配电路及公式
- [gmm+kde] target tracking simulation based on GMM and KDE kernel estimation of MATLAB
- UE4 getting started with bone animation
- 200 c language words, please collect!
- laravel8 实现阿里云文件上传
- Several reasons and solutions of virtual machine Ping failure
- Clustered and non clustered indexes
- Sdn/nfv application in cloud data center
- 线程池的创建
- 原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布
猜你喜欢

联想首次详解混合云Lenovo xCloud五大优势,如何打造智能化数字底座

kubernetes多網卡方案之Multus CNI部署和基本使用

Icml2022 | reexamine end-to-end voice to text translation from scratch

How Photoshop opens, edits and exports pictures in webp format

That's great. The Ministry of industry and information technology has launched an internet account with a "one click unbinding" mobile phone number, which can be called an artifact

Vscode common shortcuts

AI智能视频分析EasyCVR平台设备通道批量删除功能的开发实现

样板小作坊

ICML2022 | 從零開始重新審視端到端的語音到文本翻譯

Play electronics, poor three generations
随机推荐
启牛的证券是哪个证券公司的呢?安全吗?
完美解码PureCodec 20220601
That's great. The Ministry of industry and information technology has launched an internet account with a "one click unbinding" mobile phone number, which can be called an artifact
Can Huawei matepad become the secondary screen of your laptop?
Vulnhub练习 DC-1靶机
Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association
中银证券开户有什么风险吗?安全的吗?
Face recognition software based on deepface model
DC4 of vulnhub
Pulling method of common webcam
Software features and functions of the blind box mall app system development
Vscode common shortcuts
优化代码去除if-else
How to run Plink software -- three methods
How Photoshop opens, edits and exports pictures in webp format
Blue Bridge Cup_ A fool sends a letter_ recursion
MySQL相关-0416
中金财富证券证券股票开户安全吗?靠谱吗?
聚簇索引和非聚簇索引
How about opening an account with flush? Is it safe to open an account?