分享

12. Java String 类

 月小升的图书管 2020-02-23

一、String的创建方法

1. 直接创建

String str = "我是 ";
System.out.println(str);

2. 拷贝创建

String str = new String("我是 ");
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:///blog/java-string-study/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多