String.format 的大用场
来源:互联网 发布:网络塔防游戏排行 编辑:程序博客网 时间:2024/05/14 19:05
String.format 的大用场
String.format是在JDK1.5中新增的静态方法,功能强。它主要功能是格式化数据,大致分为这些类(常规类型、字符类型、数值类型、日期类型)。它的语法如下:
常规类型、字符类型和数值类型的格式说明符的语法:%[参数索引位置$][转换标识符][最小官渡][.保留精度位数]转换方式
日期语法:%[参数索引位置$][t或T]转换方式
用场:
1、将第一个参数转换为字符串,类似MessageFormat.format语法
String.format("后台流水号 %1$s","123");
返回: 后台流水号 123
2、将两个参数格式化为字符串,并连接在一起
String.format("%1$s%2$s",1234,5678);返回: 12345678
3、指向第一个参数转换为字符串
String.format("%s",1234,5678); 返回: 1234
4、将第一个格式化为“1234” 第二个格式化005678
String.format("%s%06d",1234,5678);返回: 1234005678
5、将明文替换成'*'符
String password = "howto";System.out.println(password);String result = String.format("%" + password.length() + "s", "");System.out.println(result);System.out.println(result.replace(' ', '*')); 输出: howto *****
6、将字符串拉长为20个长度,非空字符左对齐
String.format("%1$-" + 20 + "s", "Howto")输出: Howto *
7、将字符串拉长为25个长度,非空字符右对齐
String.format("%1$#" + n + "s", s)输出: Howto*
8、字符串补齐
System.out.println(String.format("%10s", "howto").replace(' ', '*'));System.out.println(String.format("%-10s", "howto").replace(' ', '*'));输出: *****howto howto*****
9、各种用法:
//转换为科学记数法表示//输出:1.234568e+09System.out.println(String.format("%1$e", 1234567890.123456789));//转换为科学记数法表示,长度为20,用0填充//输出:000000001.234568e+09System.out.println(String.format("%1$020e", 1234567890.123456789));//根据结果制动识别使用转换器e或f//输出:12345.1System.out.println(String.format("%1$g", 12345.123456789));//转换为16进制的浮点数//输出:0x1.81c8fcd6e9b9dp13System.out.println(String.format("%1$a", 12345.123456789));//转换结果保留默认小数位,3位数字用,隔开,转换为十进制表示//输出:1,234,567,890.123457System.out.println(String.format("%1$,f", 1234567890.123456789));//转换结果保留默认小数位,3位数字用,隔开//输出:1,234,567,890.123457System.out.println(String.format("%1$,f", 1234567890.123456789));//转换结果是保留10位精度.转换成十进制表示方式//输出:1234567890.1234567000System.out.println(String.format("%1$.10f", 1234567890.123456789));//转换结果是保留100位精度,没有精度丢失,整数位3个就用,隔开//输出:12,345,678,909.1234567890123456789000000000000000000000000000000000000000000000000000000000000000000000000000000000System.out.println(String.format("%1$,.100f", new BigDecimal("12345678909.1234567890123456789")));//转换结果保留5位小数,3位数字用,隔开//输出:1,234,567,890.12346System.out.println(String.format("%1$,.5f", 1234567890.123456789));//格式化成数值表示使用十进制//输出:"-31"System.out.println(String.format("%1$d", -31)); //格式化成数值表示使用八进制//输出:"37777777741"System.out.println(String.format("%1$o", -31)); //格式化成数值表示使用十进制,总长度显示19位//输出:" -31"System.out.println(String.format("%1$19d", -31));//格式化成数值表示使用十进制,总长度显示19位,左靠齐//输出:"-31 "System.out.println(String.format("%1$-19d", -31));//格式化成数值表示,使用十进制,//输出:"-00000031"System.out.println(String.format("%1$09d", -31));//每3位数字用,隔开,总长度9位,//输出:" -3,123"System.out.println(String.format("%1$,9d", -3123));//每3位数字用,隔开,用0填充总长度9位,//输出:"-0003,123"System.out.println(String.format("%1$,09d", -3123));//每3位数字用,用0填充总长度9位,//输出:" (3123)"System.out.println(String.format("%1$(9d", -3123));//输出:" -31"System.out.println(String.format("%1$ 9d", -31));//转换为字符//输出:"a"System.out.println(String.format("%1$c", 97));//转换为字符,十位//输出:" 鄒"System.out.println(String.format("%1$10c", '鄒'));//转换为字符,十位,靠左//输出:"鸿"System.out.println(String.format("%1$-10c", '鸿'));//输出:12.123456%System.out.println(String.format("%1$f%%", 12.123456));//留取4位小数,4舍5入//输出:"12.1235%"System.out.println(String.format("%1$.4f%%", 12.123456));BigDecimal a = new BigDecimal("12.12"),b = new BigDecimal("13.13");BigDecimal c = a.divide(b,28,BigDecimal.ROUND_HALF_UP);//保留28位小数//输出:0.9230769230769230769230769231System.out.println(c + "");//格式为保留28位小数//输出:0.9230769230769230769230769231System.out.println(String.format("%1$.28f", c));long c = System.currentTimeMillis();//输出:2012-05-09 15:14:28System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c));//输出:12-05-09 03:14:28 下午 五月 星期三System.out.println(String.format("%1$ty-%1$tm-%1$td %1$tI:%1$tM:%1$tS %1$tp %1$tb %1$ta", c));//输出:2009-06-14 01:39:35System.out.println(String.format("%1$tF %1$tT", 1244943575031l));//输出:"false true true true"System.out.println(String.format("%b %b %b %b", null,"","true",true));String pattern = "%1$s 在 %4$tF %4$tT 说了 \"%1$s 爱 %2$s %3$d 年\"";//输出:"mingming 在 2012-05-09 15:15:31 说了 "mingming 爱 shuilian 10000 年""System.out.println(String.format(pattern, "mingming","shuilian",10000,System.currentTimeMillis()));
0 0
- String.format 的大用场
- String.format 的大用场
- String.format 的大用场
- QD的大用场
- string.format 大括号
- SourceMonitor: 代码重构的小工具,大用场
- 大派用场的Web 开发第三方资源
- SourceMonitor: 代码重构的小工具,大用场
- SourceMonitor: 代码重构的小工具,大用场
- string.format输出大括号
- string.format输出大括号
- String.Format 源字符串包含大括号的小问题
- String.Format 源字符串包含大括号的小问题
- C++的用场
- string.format的使用
- String.format()的使用方法
- string.Format的用法
- String.format()的使用方法
- php pdo操作数据库工具类
- python读取文件里的单词,统计词频,输出到文件
- 数据结构(一)顺序表3:顺序表常见题型
- 【深度学习】针对CIFAR-10模型所写的进行图片size,图片名,后缀图片格式写的预处理程序
- Java 12
- String.format 的大用场
- React Native style属性重载
- JSch: UnknownHostKey exception
- 跑过的城市,走过的路
- MFC
- ROS语音识别-----pocketsphinx安装和使用
- 排序
- Java 13
- 求二叉树的宽度