关于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 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值