12. Java String 类

一、String的创建方法


1. 直接创建

 

String str = "我是 java-er.com";
System.out.println(str);

2. 拷贝创建

String str = new String("我是 java-er.com");
System.out.println(str);

3.用数组构建

char[] arr = {'J','a','v','a'};    
String arrString = new String(arr);  
System.out.println(arrString);

 

二、String 格式化


输出格式化数字可以使用 printf() 和 format() 方法。
1. printf() 方法为了一次打印输出格式化数字
2. String 类的静态方法 format() 能用来创建可复用的格式化字符串,而不仅仅是用于一次打印输出。

 

float f = 2.4f;
int i = 5;
String s = "字符串";
System.out.printf("浮点型变量的值为 " +
                  "%f, 整型变量的值为 " +
                  " %d, 字符串变量的值为 " +
                  "is %s", f, i, s);
 
String fs = String.format("浮点型变量的值为 " +
                  "%f, 整型变量的值为 " +
                  " %d, 字符串变量的值为 " +
                  "is %s", f, i, s);
 
System.out.println("\n我可以再次打印:" + fs);

输出

浮点型变量的值为 2.400000, 整型变量的值为  5, 字符串变量的值为 is 字符串
我可以再次打印:浮点型变量的值为 2.400000, 整型变量的值为  5, 字符串变量的值为 is 字符串

https://java-er.com/blog/java-string-study/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值