返回

String.format()方法使用

发布时间:2023-08-28 09:17:19 333


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


特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
数据流中的中位数 2023-08-28 05:57:32