String.format方法使用-淺析(轉)


轉自  https://blog.csdn.net/u010137760/article/details/82869637 

1.代碼中簡單使用
2.源碼調用的方法
3.相關類-Formatter
3.1可選的參數索引
3.2可選的標記
3.3可選的寬度
3.4可選的精度
3.5強制類型轉換
3.1非日期/時間轉換類型
3.1.1字符串轉換
3.1.2字符轉換
3.1.3整數轉換
3.1.4浮點數轉換
3.1.5布爾值轉換
3.1.6hash值轉換
3.1.7無參轉換
3.2日期/時間轉換
1.代碼中簡單使用
String.format("%.2f", 2.0f);
1
1
2.源碼調用的方法
* Returns a localized formatted string, using the supplied format and arguments,
* 返回本地化格式的字符串,使用提供的格式和參數
* using the user's default locale.
* 使用用戶默認的本地語言環境
* <p>If you're formatting a string other than for human
* consumption, you should use the {@code format(Locale, String, Object...)}
* 格式化字符串如果使用其他語言環境,你應該使用format(Locale, String, Object...)
* overload and supply {@code Locale.US}. See
* "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
*
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}

public static String format(Locale locale, String format, Object... args) {
if (format == null) {
throw new NullPointerException("format == null");
}
int bufferSize = format.length() + (args == null ? 0 : args.length * 10);
Formatter f = new Formatter(new StringBuilder(bufferSize), locale);
return f.format(format, args).toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
3.相關類-Formatter
在Formatter類的釋義中有如下信息:

Format strings consist of plain text interspersed with format specifiers, such as {@code “name: %s weight: %03dkg\n”}. Being a Java string, the usual Java string literal backslash escapes are of course available.

格式字符串由純文本組成,其中穿插了格式說明符,如"name: %s weight: %03dkg\n"中的%s、%03d。作為java字符串,反斜杠轉移符也是支持的

Format specifiers (such as {@code “%s”} or {@code “%03d”} in the example) start with a {@code %} and describe how to format their corresponding argument. It includes an optional argument index, optional flags, an optional width, an optional precision, and a mandatory conversion type.

格式說明符以%開頭,描述如何格式化相應參數。格式說明符包括可選的參數索引、可選的標記、可選的寬度、可選的精度、強制類型轉換

3.1可選的參數索引
標記 作用 例子 結果
%2$s中的2指定顯示第2個參數 format("%2s, s,%1s,s",“小明”,“小李”) 小李,小明
< 復用同一個參數 format("%o %<d %<x", 64); 100 64 40
3.2可選的標記
標記 作用 例子 結果
, 分割大數據 format("%,d", 1024); 1,234
+ 總是顯示正數、負數符號 format("%+d, %+4d", -5, 5); -5,   +5
非負數有一個前導空格 String.format(“x% d% 5d”, 4, -4); x 4   -4
( 把括號括在負數上 format("%(d, %(d, %(6d", 12, -12, -12); 12, (12),   (12)
- 對齊
需要寬度;
-默認右對齊,-號左對齊; format("%-6dx", 5);
format("%-3C,%3C", ‘d’, 0x65); 5     x
D  ,  E
0 用前導零填充數字 format("%07d, %03d", 4, 5555); 0000004, 5555
# 變形
僅八進制、十六進制; format("%o %#o %d", 010, 010,010);
format("%x %#x %d", 0x12, 0x12,0x12); 10 010 8
12 0x12 18
3.3可選的寬度
The width is a decimal integer specifying the minimum number of characters to be used to represent the argument.

寬度是一個十進制整數,指定參數顯示的最小字符數。
注:不能使用寬度來截斷字段

標記 作用 例子 結果
5前面有兩個空格 format("%3d",5);   5
3.4可選的精度
The precision is a {@code .} followed by a decimal integer

精度是.后面跟的整形數字

標記 作用 例子 結果
. 小數點后面跟幾位 format("%.1f", 123.456f); 123.5
3.5強制類型轉換
3.1非日期/時間轉換類型
3.1.1字符串轉換
標記 作用 例子 結果
s 字符串 format("%s %s", “hello”, “Hello”); hello Hello
S 大寫字符串 format("%S %S", “hello”, “Hello”); HELLO HELLO
3.1.2字符轉換
標記 作用 例子 結果
c 字符 format("%c %c", ‘d’, ‘E’); d E
C 大寫字符 format("%C %C", ‘d’, ‘E’); D E
3.1.3整數轉換
標記 作用 例子 結果
d 十進制 format("%d", 26); 26
o 八進制 format("%o", 032); 32
x 十六進制 format("%x %X", 0x1a, 0x1a); 1a 1A
3.1.4浮點數轉換
標記 作用 例子 結果
f 十進制小數 format("%f", 123.456f);
format("%.1f", 123.456f);
format("%1.5f", 123.456f);
format("%10f", 123.456f);
format("%6.0f", 123.456f); 123.456001
123.5
123.45600
123.456001
   123
e,E 指數浮點數 format("%e", 123.456f);
format("%.1e", 123.456f);
format("%1.5E", 123.456f);
format("%10E", 123.456f);
format("%6.0E", 123.456f); 1.234560e+02
1.2e+02
1.23456E+02
1.234560E+02
 1E+02
g,G 十進制或指數取決於數的大小 format("%g %g", 0.123, 0.0000123); 0.123000 1.23000e-05
a,A 十六進制小數 format("%a", 123.456f); 0x1.edd2f2p6
3.1.5布爾值轉換
支持Boolean值。null被認為是false,其他類型實例為true

標記 作用 例子 結果
b,B 布爾值 format("%b %b", true, false);
format("%B %B", true, false);
format("%b", null);
format("%b", “hello”); true false
TRUE FALSE
false
true
3.1.6hash值轉換
調用參數的hashCode方法,支持所有類型

標記 作用 例子 結果
h,H 十六進制hashcode format("%h", this);
format("%H", this);
format("%h", null); 190d11
190D11
null
3.1.7無參轉換
標記 作用 例子 結果
% 輸出%字符 format("%d%%", 50); 50%
n 換行 format(“first%nsecond”); first\nsecond
3.2日期/時間轉換
日歷、日期、毫秒值均可以作為日期/時間參數。如果類型有錯誤,則返回1970-01-01 00:00:00 UTC

標記 作用 例子 結果
ta 周內第幾天(縮寫) format("%ta", cal, cal); Tue
tA 周內第幾天(全) format("%tA", cal, cal); Tuesday
tb 月份第幾月(縮寫)) format("%tb", cal); Apr
tB 月份第幾月(全) format("%tB", cal); April
tc 時間,請勿使用 format("%tc", cal); Tue Apr 01 16:19:17 CEST 2008
tC 世紀,兩位數 format("%tC", cal); 20
td 月份第幾天,兩位數 format("%td", cal); 01
tD 美國日期格式,請勿使用 format("%tD", cal); 04/01/08
te 月中的1天(1-31) format("%te", cal); 1
tF ISO 8601標准下的全面日期格式(YYYY-MM-DD) format("%tF", cal); 2008-04-01
th %tb的同義詞
tH 2位數表示小時,24小時制(00-23) format("%tH", cal); 16
tI 2位數表示小時,12小時制(01-12) format("%tI", cal); 04
tj 三位數表示一年中的第幾天(001-366) format("%tj", cal); 092
tk 表示小時,24小時制(0-23) format("%tk", cal); 16
tl 表示小時,12小時制(1-12) format("%tl", cal); 4
tL 毫秒數 format("%tL", cal); 359
tm 2位數一年中的第幾個月(01-12) format("%tm", cal); 04
tM 2位數分鍾 format("%tM", cal); 08
tN 納秒 format("%tN", cal); 359000000
tp 上午或下午 format("%tp %Tp", cal, cal); pm PM
tQ 自時代以來的毫秒值 format("%tQ", cal); 1207059412656
tr 完整的12小時時間 %tI:%tM:%tS %Tp format("%tr", cal); 04:15:32 PM
tR 短時24小時制 format("%tR", cal); 16:15
ts 自時代以來的秒值 format("%ts", cal); 1207059412
tS 兩位數秒(00-60) format("%tS", cal); 17
tT 完整24小時制 %tH:%tM:%tS format("%tT", cal); 16:15:32
ty 兩位數年(00-99) format("%ty", cal); 08
tY 四位數年 format("%tY", cal); 2008
tz 時間區格林尼治時間偏移 format("%tz", cal); +0100
tZ 本地時區縮寫 format("%tZ", cal); CEST

---------------------
作者:洛漠O_o
來源:CSDN
原文:https://blog.csdn.net/u010137760/article/details/82869637
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM