String.format()_纪要

70 篇文章 0 订阅
8 篇文章 0 订阅
/**
 * String.format()纪要
 */ 
public class FormatTest {

	public static void main(String[] args) {  
		
		/**
	     * 替换函数
	     * String.format();
		 * %s: 字符串类型数据替换;
	     * %c: 字符类型数据替换;
	     * %n: 换行符;
	     * %b: Boolean类型;(表达式计算)
	     * %d: 整数类型;(表达式计算)
	     * %x: 16进制整数类型;
	     * %o: 8进制整数类型;
	     * %f: 浮点类型;
	     * %a: 十六进制浮点类型;
	     * %e: 指数类型;
	     * %g: 通用浮点类型(f和e类型中较短的);
	     * %%: 百分比类型;
	     * %h: 散列码;
	     * 
		 */
	    System.out.println(String.format("Hello %s %s %s", "男一号","走秀一号","蓝擦号"));  
	    System.out.println(String.format("Hello%s%s%s", "男一号","走秀一号","蓝擦号"));  
	    System.out.println(String.format("Hello:%s|%s|%s", "男一号","走秀一号","蓝擦号"));  
	    System.out.println(String.format("Hello:%s,%s,%s", "男一号","走秀一号","蓝擦号"));  
		System.out.printf("%c %n %c", 'A', 'B');  
		System.out.printf("3>7?%b", 3>7);  
		System.out.printf("%d", 100/2);  
		System.out.printf("100的16进制数=%x", 100);  
		System.out.printf("100的8进制数=%o", 100);  
		System.out.printf("100元的书打3.8折=%f元", 100*3.8);  
		System.out.printf("十六进制浮点类型=%a", 100*3.8);  
	    System.out.printf("指数类型%e", 100*3.8);  
		System.out.printf("通用浮点类型(f和e类型中较短的)=%g", 100*3.8);  
	    System.out.printf("%d%%", 85);  
	    System.out.printf("字母A的散列码=%h", 'A');  
	}
    
}

转  换  符

说    明 

示    例

%s

字符串类型

"mingrisoft"

%c

字符类型

'm'

%b

布尔类型

true

%d

整数类型(十进制)

99

%x

整数类型(十六进制)

FF

%o

整数类型(八进制)

77

%f

浮点类型

99.99

%a

十六进制浮点类型

FF.35AE

%e

指数类型

9.38e+5

%g

通用浮点类型(f和e类型中较短的)

 

%h

散列码

 

%%

百分比类型

%n

换行符

 

%tx

日期与时间类型(x代表不同的日期与时间转换符

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值