formato especificador de formato sintaxis:
%[argument_index$][flags][width][.precision]conversion
Análisis gramatical:
[argument_index$]: "1$" 第1个参数
[flags]: "0" , "" 填充字符,数字用"0"或"",字符串用""
[width]: "10","-10" 最后总宽度10,正数表示在前面添加,负数表示在后面添加。负数的情况一般只在字符串时才用
[.precision] : ".3" 小数点后保留3位小数,必须是浮点数类型参数才行
conversion: d 代表整数,f代表浮点数,s代表字符串,类型要匹配才行,否则报错 还有t转义的很多字符
Ejemplo de formato:
1. Formatee un número entero, la cantidad de dígitos no es suficiente para completar con 0 (\d entero solo puede tener 10 dígitos de longitud)
String.format("%010d",123);//会转换成0000000123
2. Formatee un número de coma flotante, agregue 0 hacia adelante si el número entero no es suficiente y agregue 0 hacia atrás si el lugar decimal no es suficiente
String.format("%010.3f", new BigDecimal(123.0)).replace(".",""));
//(注意如果整个长度为9位.则长度要填写10(因为有.号)
3. Dar formato a una cadena. El número de dígitos no es suficiente para llenar los espacios en blanco
String.format("%10s","abc");//(注意该方法不能格式化中文)
格式化含中文的用以下方法:
String content="java格式化";
String blank=" ";//一定长度空格
(content+blank).substring(0, 20- content.getBytes().length);
4. Fecha de formato (el carácter de sufijo se define para la conversión 't' y 'T')
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"。
Código de prueba:
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
}
}
expandir
-
Los espacios se muestran en formato String.format:
 ;
(representa medio espacio) (los espacios en html están
representados por ) -
Nueva línea en formato String.:
\n
-
Cadena.formato中TAb:
\t
-
特殊字符(如:@ 、%)
La forma de escribir: la escritura directa informará un error, necesita用\转义
-
Si no es necesario formatear su cadena, puede agregar un atributo a su etiqueta <string:
formatted="false"
por ejemplo:<string name="test" formatted="false">% test %</string>
. -
%1$s,%1$d
Uso de etc
%n$ms
.: la salida es una cadena, n es el parámetro numérico, puede poner un espacio antes de la salida al establecer el valor de m
%n$md
: la salida es un número entero, n es el parámetro numérico, el valor de m puede ser establecer Ponga un espacio antes de la salida, o configúrelo en 0m, ponga m 0s antes de la salida
%n$mf
: significa que la salida es un número de punto flotante, n significa el número de parámetros, establecer el valor de m puede controlar el número de lugares decimales, como cuando m=2.2, el formato de salida es 00.00También se puede escribir simplemente como:
%d
(representa un número entero)
%f
(representa un número de coma flotante)
%s
(representa una cadena)