format format specifier syntax:
%[argument_index$][flags][width][.precision]conversion
Grammar analysis:
[argument_index$]: "1$" 第1个参数
[flags]: "0" , "" 填充字符,数字用"0"或"",字符串用""
[width]: "10","-10" 最后总宽度10,正数表示在前面添加,负数表示在后面添加。负数的情况一般只在字符串时才用
[.precision] : ".3" 小数点后保留3位小数,必须是浮点数类型参数才行
conversion: d 代表整数,f代表浮点数,s代表字符串,类型要匹配才行,否则报错 还有t转义的很多字符
Formatting example:
1. Format an integer, the number of digits is not enough to fill forward with 0 (\d integer can only be 10 digits in length)
String.format("%010d",123);//会转换成0000000123
2. Format a floating-point number, add 0 forward if the integer is not enough, and add 0 backward if the decimal place is not enough
String.format("%010.3f", new BigDecimal(123.0)).replace(".",""));
//(注意如果整个长度为9位.则长度要填写10(因为有.号)
3. Format a string. The number of digits is not enough to fill in the blanks
String.format("%10s","abc");//(注意该方法不能格式化中文)
格式化含中文的用以下方法:
String content="java格式化";
String blank=" ";//一定长度空格
(content+blank).substring(0, 20- content.getBytes().length);
4. Format date (suffix character is defined for 't' and 'T' conversion)
String.format("%tY", new Date()); //2011
String.format("%tm", new Date()); //03
String.format("%tF", new Date()); //2011-03-04
String.format("%tR", new Date()); //15:49
String.format("%tT", new Date()); //15:49:34
String.format("%tc", new Date()); //星期五 三月 04 15:49:34 CST 2011
String.format("%tD", new Date()); //03/04/11
String.format("%td", new Date()); //04
以下转换字符用来格式化日期:
'B' 特定于语言环境的月份全称,例如 "January" 和 "February"。
'b' 特定于语言环境的月份简称,例如 "Jan" 和 "Feb"。
'h' 与 'b' 相同。
'A' 特定于语言环境的星期几全称,例如 "Sunday" 和 "Monday"
'a' 特定于语言环境的星期几简称,例如 "Sun" 和 "Mon"
'C' 除以 100 的四位数表示的年份,被格式化为必要时带前导零的两位数,即 00 - 99
'Y' 年份,被格式化为必要时带前导零的四位数(至少),例如,0092 等于格里高利历的 92 CE。
'y' 年份的最后两位数,被格式化为必要时带前导零的两位数,即 00 - 99。
'j' 一年中的天数,被格式化为必要时带前导零的三位数,例如,对于格里高利历是 001 - 366。
'm' 月份,被格式化为必要时带前导零的两位数,即 01 - 13。
'd' 一个月中的天数,被格式化为必要时带前导零两位数,即 01 - 31
'e' 一个月中的天数,被格式化为两位数,即 1 - 31。
以下转换字符用于格式化常见的日期/时间组合。
'R' 24 小时制的时间,被格式化为 "%tH:%tM"
'T' 24 小时制的时间,被格式化为 "%tH:%tM:%tS"。
'r' 12 小时制的时间,被格式化为 "%tI:%tM:%tS %Tp"。上午或下午标记 ('%Tp') 的位置可能与语言环境有关。
'D' 日期,被格式化为 "%tm/%td/%ty"。
'F' ISO 8601 格式的完整日期,被格式化为 "%tY-%tm-%td"。
'c' 日期和时间,被格式化为 "%ta %tb %td %tT %tZ %tY",例如 "Sun Jul 20 16:17:00 EDT 1969"。
Test code:
import java.util.Date;
public class StringTest {
public static void main(String[] args) {
//为了能清楚地表示" ",我把结果中的空格换成了"@"
//整数 必须是整数才能转换
System.out.println(String.format("%010d",123)); //0000000123
System.out.println(String.format("%2$08d", -3123,-5566)); //-0005566
System.out.println(String.format("%1$9d", -31)); //@@@@@@-31
System.out.println(String.format("%1$09d", -31)); //-00000031
//数字分割
System.out.println(String.format("%,d",Integer.MAX_VALUE)); //2,147,483,647
System.out.println(String.format("%,f",Float.MAX_VALUE)); //340,282,346,638,528,860,000,000,000,000,000,000,000.000000
//浮点数 必须是浮点数才能转换
System.out.println(String.format("%020.3f", 12.3)); //0000000000000012.300
System.out.println(String.format("%1$.2f", 5689.0)); //5689.00
//字符串 和 单字符c
System.out.println(String.format("%-10s", "sss")); //sss@@@@@@@
System.out.println(String.format("%10s", "sss")); //@@@@@@@sss
System.out.println(String.format("%5c", 'v')); //@@@@v
//综合 日期处理
String s3= String.format("你的生日:%1$tF,现在的时间:%2$tT", new Date(),System.currentTimeMillis());
System.out.println(s3); //你的生日:2015-01-30,现在的时间:22:58:24
//格式化的位置
String str = "I love ni %s, you love me %s"; //当不标明参数时,将按顺序替换
String str2 = "I love ni %2$s, you love me %1$s";
System.out.println(String.format(str, "renjunjie","songliyu")); //I love ni renjunjie, you love me songliyu
System.out.println(String.format(str2, "renjunjie","songliyu")); //I love ni songliyu, you love me renjunjie
//数组的操作
Object[] sendData = new Object[4];
sendData[0] = Integer.valueOf(1);
sendData[1] = "172.12.1.2";
sendData[2] = Integer.valueOf(123);
sendData[3] = "testadfaerfa";
String sendDataString = String.format("%d,%s,%d,%s",(Object[]) sendData); //当不标明参数时,将按顺序替换
System.out.println(sendDataString); //1,172.12.1.2,123,testadfaerfa
}
}
expand
-
Spaces are displayed in String.format:
 ;
(represents half a space) (spaces in html are
represented by ) -
Newline in String.format:
\n
-
String.format中TAb:
\t
-
特殊字符(如:@ 、%)
The way of writing: direct writing will report an error, need用\转义
-
If your string does not need to be formatted, you can add an attribute to your <string tag:
formatted="false"
for example:<string name="test" formatted="false">% test %</string>
. -
%1$s,%1$d
The usage of etc
%n$ms
.: represents the output is a string, n represents the number of parameters, setting the value of m can place a space before the output:
%n$md
represents the output is an integer, n represents the number of parameters, setting the value of m can be in Put a space before the output, or set it to 0m, put m 0s before the output
%n$mf
: it means that the output is a floating point number, n means the number of parameters, setting the value of m can control the number of decimal places, such as when m=2.2, The output format is 00.00It can also be simply written as:
%d
(represents an integer)
%f
(represents a floating point number)
%s
(represents a string)