關於Java 佔位符


import java.text.MessageFormat;
import java.util.Date;
 
public class test01 {
 
	public static void main(String[] args) {
		System.out.println("hello");// print hello
 
		// %s佔位符,輸出字符串
		String username = "user1";
		int num = 3;
		System.out.printf("%s您好,您是第%s位訪客\n", username, num); // prints user1您好,您是第3位訪客
 
		// %f佔位符
		double d = 1.2;
		float f = 1.2f;
		System.out.printf("%f %f", d, f); // prints 1.200000 1.200000
		
 
		// %1$s佔位符
		//%n$ms:代表輸出的是字符串,n代表是第幾個參數,設置m的值可以在輸出之前放置空格,也可以設爲0m,在輸出之前放置m個0 
		System.out.println(String.format("我是%1$s,我來自%2$s,今年%3$s歲", "中國人", "北京",
				"22"));
		// prints 我是中國人,我來自北京,今年22歲
		
 
		// {}佔位符,{}內的數字代表第幾個參數,參數從0開始
		String url = "www.baidu.com";
		int count = 1000;
		System.out.println(MessageFormat
				.format("該網站{0}被訪問了 {1} 次.", url, count));
		// prints 該網站www.baidu.com被訪問了 1,000 次.
 
 
		// {}佔位符
		String template = "Welcome {0}!  Your last login was {1}";
		String output = MessageFormat.format(template, new Object[] { "Python",
				new Date().toString() });
		System.out.println(output);
		// prints  Welcome Python!  Your last login was Fri Oct 10 20:47:00 CST 2014
 
	}
 

--------------------- 
作者:gsls200808 
來源:CSDN 
原文:https://blog.csdn.net/gsls200808/article/details/39968749 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章