String.format()方法使用

相关标签:
package com.codewars;
import java.util.Date;
/**
* Created by MaYanChao on 2016/10/12.--->>>Buddha Bless, No Bug !
* //
* // _ooOoo_
* // o8888888o
* // 88" . "88
* // (| -_- |)
* // O\ = /O
* // ____/`---'\____
* // .' \\| |// `.
* // / \\||| : |||// \
* // / _||||| -:- |||||- \
* // | | \\\ - /// | |
* // | \_| ''\---/'' | |
* // \ .-\__ `-` ___/-. /
* // ___`. .' /--.--\ `. . __
* // ."" '< `.___\_<|>_/___.' >'"".
* // | | : `- \`.;`\ _ /`;.`/ - ` : | |
* // \ \ `-. \_ __\ /__ _/ .-` / /
* // ======`-.____`-.___\_____/___.-`____.-'======
* // `=---='
* // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
public class Format {
public static void main (String[] args){
System.out.println(String.format("我叫%2$s,她叫%1$s","Jack","Peter"));//我叫Peter,她叫Jack
System.out.println(String.format("我的名字叫%s", "小明")); // 打印:我的名字叫小明
String.format("我叫%s,她叫%s", "小明","小方"); // 我叫小明,她叫小方
System.out.println(String.format("%o", 8)); //格式化为八进制
System.out.println(String.format("%x", 16));//格式化为十六进制
System.out.println(String.format("%1$,d", 12302562));
System.out.println(String.format("%1$08d", 123456));
System.out.println(String.format("%1$.2f", 12.12555));
System.out.println(String.format("%1$tY-%1$tm-%1$te", new Date()));
}
}
输出结果:
"C:\Program Files\Java\jdk1.8.0_92\bin\java" -Didea.launcher.port=7538 com.intellij.rt.execution.application.AppMain com.codewars.Format
我叫Peter,她叫Jack
我的名字叫小明
10
10
12,302,562
00123456
12.13
2016-10-12
Process finished with exit code 0
文章来源: https://blog.51cto.com/u_13618048/5891587
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报