5.1 String 类
5.1.1 声明字符串
字符串是常量,它们可以显示任何文字信息,
‘ ’ 字符 一个
“ ” 字符串 多个
5.1.2 创建字符串
例5.1
public class zifuchuan5_1 {
public static void main(String[] args) {//主函数
String a = "时间就是金钱,我的朋友。";//直接引用字符串常量
System.out.println("a =" + a);//输出a
String b = new String("我爱清汤小肥羊");//利用构造方法实例化
String c = new String(b);//使用已有字符串变量实例化
System.out.println("b =" + b);//输出b
System.out.println("c =" + c);//输出c
char[] charArray = { 't','i','m','e' };//定义一维数组
String d = new String(charArray);//利用字符数组实例化
System.out.println("d =" + d);//输出d
char[] charArray2 = { '时','间','就','是','金','钱' };//定义一维数组
String e = new String(charArray2, 4, 2);//提取数组部分内容,从下标为4的元素开始,截取2个字符
System.out.println("e =" + e);//输出e
}
}
5.2 连接字符串
5.2.1 连接字符串
String a =“建团“
String b =“一百周年“
b= a + b; // b =“建团一百周年“
b += a ; // b =b+a=“建团一百周年“
例5.2
public class zifuchuan5_2 {
public static void main(String[] args) {
String a = "abc";//直接引用字符串常量
String b = "123";//直接引用字符串常量
String c = a + b +"!";//使用“+”拼接字符串
String d = "拼接字符串";//直接引用字符串常量
d += c;//使用“+=”拼接字符串
System.out.println("a =" + a);//输出a
System.out.println("b =" + b);//输出b
System.out.println("c =" + c);//输出c
System.out.println("d =" + d);//输出d
}
}
5.2.2连接其他数据类型
例5.3
public class zifuchuan5_3 {
public static void main(String[] args) {//主函数
int booktime = 4;//声明int型变量booktime
float practice = 2.5f;//声明float型变量practice
System.out.println("我每天花费" + booktime + "小时看书;" + practice + "小时上机练习");
//将字符串与整型、浮点型变量相连,并将结果输出
}
}
5.3提取宇符申信息
5.3.1 获取字符串长度
长度 字符串名.length();
String a = “建团一百周年”
Int b = a. length();
5.3.2 获取指定的字符
String a = “建团一百周年”
Char b = a. chrAt (3);
例5.4
public class zifuchuan5_4 {
public static void main(String[] args) {
String str = "床前明月光,疑是地上霜。";//创建字符串对象str
char chr = str.charAt(4);//将字符串str中索引位置为4的字符赋值给chr
System.out.println("字符串中索引位置为4的字符是:" + chr);//输出chr
}
}
5.3.3 获取子字符串索引位置
String a = “建团一百周年”
Int b = a.indexof(“百”); //3
String a = “abacadaeaf”
例5.5
public class zifuchuan5_5 {
public static void main(String[] args) {
String str="12345abcde";//创建字符串对象
int charIndex = str.indexOf("abc");//获取字符串str中“abc”首次出现的索引,赋值给charIndex
if (charIndex !=-1) {//判断:index的值不等于-1
//如果index不等于-1,则执行此行代码,说明str中存在“abc”字符串
System.out.println("str中存在abc字符串");//输出
} else {// 如果 index 等于-1,则执行此行代码,说明 str 中没有“abc”字符事
System.out.println("str中没有abc字符串");//输出
}
}
}
例5.6
public class zifuchuan5_6 {
public static void main(String[] args) {
String str = "We are the world";//创建字符串
int firstIndex = str.indexOf("r");//获取字符串中“r”第一次出现的索引位置
int secondIndex = str.indexOf("r",firstIndex + 1);
//获取字符串中“r”第二次出现的索引位置,从第一次出现的索引位置之后开始查找
int thirdIndex = str.indexOf("r", secondIndex + 1);
//获取字符串中“r”第三次出现的索引位置,从第二次出现的索引位置之后开始查找
System.out.println("r第一次出现的索引位置是:" + firstIndex); //输出第一次获取的索引位置
System.out.println("r第二次出现的索引位置是:" + secondIndex);//输出第二次获取的索引位置
System.out.println("r第三次出现的索引位置是:" + thirdIndex );//输出第三次获取的索引位置
}
}
例5.7
public class zifuchuan5_7 {
public static void main(String[] args) {
String str = "Let it go!Let it go!"; // 创建字符串对象
int gIndex = str.lastIndexOf("g"); // 返回”g“最后一次出现的位置
int goIndex = str.lastIndexOf("go"); // 返回”go“最后一次出现的位置
int oIndex = str.lastIndexOf("o"); // 返回”o“最后一次出现的位置
System.out.println("字符串\"Let it go!Let it go\"中:\n");//输出
System.out.println("\"g\"最后一次出现的位置是:" + gIndex);//输出
System.out.println("\"o\"最后一次出现的位置是:" + oIndex);//输出
System.out.println("\"go\"最后一次出现的位置是:" + goIndex);//输出
}
}
例5.8
public class zifuchuan {
public static void main(String[] args) {
String str = "01a3a56a89";
// 返回字母"a"最后一次出现的索引位置
int lastIndex = str.lastIndexOf("a");
// 返回字母"a"的索引位置otherIndex
// 满足0<=fiveBeforeIndex<=5条件,在满足条件的结果集中,返回最大的数字
int fiveBeforeIndex = str.lastIndexOf("a", 5);
// 返回字母"a"的索引位置otherIndex
// 满足0<=threeBeforeIndex<=3条件,在满足条件的结果集中,返回最大的数字
int threeBeforeIndex = str.lastIndexOf("a", 3);
System.out.println("字符串\"01a3a56a89\"中:\n");//输出
System.out.println("字母\"a\"最后一次出现的位置是:" + lastIndex);//输出
System.out.println("从索引位置5开始往回搜索,字母\"a\"最后一次出现的位置:" + fiveBeforeIndex);//输出
System.out.println("从索引位置3开始往回搜索,字母\"a\"最后一次出现的位置:" + threeBeforeIndex);//输出
}
}
5.3.4 判断字符串首尾内容
String jiantuan = "建团一百周年";
boolean firstBool = jiantuan.startsWith("建团"); //判断是否以“建团”开头
boolean firstBool = jiantuan.startsWith("建团",3);//从第3位开始判断是否以“建团”开头
boolean secondBool = jiantuan.endsWith("建团");//判断是否以“建团”结束
例5.9
public class zifuchuanzj {
public static void main(String[] args) {
// 前半句
String myDream1 = "我有一个梦想,幽谷上升,高山下降;";
// 后半句
String myDream2 = "坎坷曲折之路成坦途,圣光披露,满照人间。";
// 打印整句话
System.out.println(myDream1 + myDream2 + "\n\t\t————马丁•路德金《我有一个梦想》\n");
// 判断前半句是否以"我有一个梦想"为前缀
boolean firstBool = myDream1.startsWith("我有一个梦想");
// 判断后半句是否以"我有一个梦想"为前缀
boolean secondBool = myDream2.startsWith("我有一个梦想");
// 判断前半句的逻辑结果
if (firstBool) {
System.out.println("前半句是以\"我有一个梦想\"开始的。");
// 判断后半句的逻辑结果
} else if (secondBool) {
System.out.println("后半句是以\"我有一个梦想\"开始的。");
// 如果没有符合条件的字符串
} else {
System.out.println("没有以\"我有一个梦想\"开始的。");
}
}
}
例5.10
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串对象
String str = "床前明月光,疑是地上霜。\n举头望明月,低头思故乡。";
// 打印古诗
System.out.println(" 《静夜思》\n" + str + "\n");
// 返回换行符所在的位置
int enterIndex = str.indexOf("\n");
// 返回从换行符之后开始的子字符串前缀是否为”举“。
// 换行符在字符串中只占一个字符,所以enterIndex + 1
boolean flag = str.startsWith("举", enterIndex + 1);
if (flag) {
// 如果结果为真,则输出此句
System.out.println("第二行是以\"举\"开始的");
} else {
// 如果结果为假,则输出第二行开头第一个字符
System.out.println("第二行是以\"" + str.charAt(enterIndex + 1) + "\"开始的");
}
}
}
例5.11
public class zifuchuanzj {
public static void main(String[] args) {
String str1 = "你说完了吗?";
String str2 = "我说完了。";
boolean flag1 = str1.endsWith("。");// 判断str1是否以"。"结尾
boolean flag2 = str2.endsWith("。");// 判断str2是否以"。"结尾
System.out.println("字符串str1是以句号结尾的吗?" + flag1);// 输出结果
System.out.println("字符串str2是以句号结尾的吗?" + flag2);
}
}
5.3.5 获取字符数组
char[] ch = 字符串名. toCharArray();
String a = "建团一百周年";
char[] ch = str.toCharArray();
例5.12
public class zifuchuanzj {
public static void main(String[] args) {
String str = "这是一个字符串";//创建一个字符串
char[] ch = str.toCharArray();//将字符串转换成字符数组
for (int i = 0; i < ch.length; i++) {//遍历字符数组
System.out.println("数组第" + i + "个元素为:" + ch[i]);//输出数组的元素
}
}
}
5.3.6 判断子字符串是否存在
boolean b = 字符串名.contains(字串);
String a = "建团一百周年";
boolean b = a.contains(一百);
例5.13
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串
String str = "今天的菜单有:蒸羊羔,蒸熊掌,蒸鹿尾。烧花鸭,烧雏鸡,烧子鹅,卤煮咸鸭,酱鸡,腊肉,松花小肚。";
System.out.println(str);// 输出字符串
boolean request1 = str.contains("腊肉");// 判断字符串中是否有“腊肉”的字样
System.out.println("今天有腊肉吗?" + request1);
boolean request2 = str.contains("汉堡");// 判断字符串中是否有“汉堡”的字样
System.out.println("今天有汉堡吗?" + request2);
}
}
例5.14
public class zifuchuanzj {
public static void main(String[] args) {
String str = "为革命保护视力,眼保健操开始!";
String substr = str.substring(8); //从第8位开始截取字符串
System.out.println("字符串str的后半句是:" + substr);
}
}
例5.15
public class zifuchuanzj {
public static void main(String[] args) {
String str = "闭门造车,出门合辙。";
// 从0开始(即从头开始)截取至4-1索引位置的子字符串
String substr = str.substring(0, 4);
System.out.println("字符串str的前半句是:" + substr);
}
}
例5.16
public class zifuchuanzj {
public static void main(String[] args) {
String str="明月几时有,把酒问青天";
String restr=str.replace("月", "日"); //将str中的”月“全部替换成”日“
System.out.println("字符串str替换之后的效果:"+restr);
}
}
例5.17
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串,前十位是数字
String str = "0123456789abc\\d";
// 使用replace()将符合"\\d"表达式的字符串替换"?"
String restr = str.replace("\\d", "?");
// 使用replaceAll()将符合"\\d"表达式的字符串替换"?"
String restrAll = str.replaceAll("\\d", "?");
// 输出结果
System.out.println("字符串str:" + str);
System.out.println("使用replace()替换的结果:" + restr);
System.out.println("使用replaceAll()替换的结果:" + restrAll);
}
}
例5.18
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串
String str = "8I want to marry you, so I need you!";
// 将开头的数字替换成两个双引号""
String noNumber = str.replaceFirst("\\d", "");
// 将第一次出现的"you"替换成"her"
String youToHer = noNumber.replaceFirst("you", "her");
// 输出结果
System.out.println("替换之后的结果是:"+youToHer);
}
}
例5.19
public class zifuchuanzj {
public static void main(String[] args) {
// 创建一个字符串
String str = "从前有座山,山里有个庙,庙里有个小松鼠";
// 让字符串按照","进行分割
String[] strArray = str.split(",");
// 使用for循环,循环输出数字所有元素
for (int i = 0; i < strArray.length; i++) {
System.out.println("数组第" + i + "索引的元素是:" + strArray[i]);//输出
}
}
}
例5.20
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串,包含多种类型字符
String str = "a1b2,c,d e f|gh";
// 使用","分割
String[] a1 = str.split(",");
// 使用空格分割
String[] a2 = str.split(" ");
// 使用"|"分割
String[] a3 = str.split("\\|");
// 使用正则表达式分割,本行用数字分割
String[] a4 = str.split("\\d");
// 同时用","、空格、"|"、数字分割,用符号"|"连接所有分隔符
String[] a5 = str.split(",| |\\||\\d");
// 显示str的原值
System.out.println("str的原值:[" + str + "]");
// 使用for-each循环展示","分割的结果
System.out.print("使用\",\"分割:");
for (String b : a1) {
System.out.print("[" + b + "]");
}
System.out.println(); // 换行
// 使用for-each循环展示空格分割的结果
System.out.print("使用空格分割:");
for (String b : a2) {
System.out.print("[" + b + "]");
}
System.out.println();
// 使用for-each循环 展示"|"分割的结果
System.out.print("使用\"|\"分割:");
for (String b : a3) {
System.out.print("[" + b + "]");
}
System.out.println();
// 使用for-each循环展示数字分割的结果
System.out.print("使用數字分割:");
for (String b : a4) {
System.out.print("[" + b + "]");
}
System.out.println();
// 使用for-each循环展示所有分隔符同时分割的结果
System.out.print("同時使用所有分隔符:");
for (String b : a5) {
System.out.print("[" + b + "]");
}
System.out.println();
}
}
例5.21
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串
String str = "192.168.0.1";
// 按照"."进行分割
String[] firstArray = str.split("\\.");
// 按照"."进行两次分割
String[] secondArray = str.split("\\.", 2);
// 输出str原值
System.out.println("str的原值为:[" + str + "]");
// 输出全部分割的结果
System.out.print("全部分割的结果:");
for (String a : firstArray) {
System.out.print("[" + a + "]");
}
System.out.println();// 换行
// 输出分割两次的结果
System.out.print("分割两次的结果:");//输出
for (String a : secondArray) {
System.out.print("[" + a + "]");//输出
}
System.out.println();
}
}
例5.22
public class zifuchuanzj {
public static void main(String[] args) {
String str = "abc DEF"; // 创建字符串
System.out.println(str.toLowerCase()); // 按照小写格式输出
System.out.println(str.toUpperCase()); // 按照大写格式输出
}
}
例5.23
public class zifuchuanzj {
public static void main(String[] args) {
String str = " abc ";//创建字符串
String shortStr = str.trim();//使用方法
System.out.println("str的原值是:[" + str + "]");//输出
System.out.println("去掉首尾空白的值:[" + shortStr + "]");//输出
}
}
例5.24
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串
String str = " a b cd e f g ";
// 利用正则表达式,将字符串中所有的空白内容都替换成””
String shortstr = str.replaceAll("\\s", "");
// 输出结果
System.out.println("str的原值是:[" + str + "]");//s输出
System.out.println("删除空内容之后的值是:[" + shortstr + "]");//输出
}
}
例5.25
public class zifuchuanzj {
public static void main(String[] args) {
String tom, jerry;
// 直接引入字符串常量
tom = "I am a student";
jerry = "I am a student";
System.out.println("直接引入字符串常量的比较结果:" + (tom == jerry));//输出
// 使用new创建信对象
tom = new String("I am a student");
jerry = new String("I am a student");
System.out.println("使用new创建对象的比较结果:" + (tom == jerry));//输出
}
}
例5.26
public class zifuchuanzj {
public static void main(String[] args) {
String str1 = "Hello";//创建字符串
String str2 = new String("Hello");//创建字符串
String str3 = new String("你好");//创建字符串
String str4 = str2;//赋值
System.out.println("str1 == str2 的结果:" + (str1 == str2));//输出
System.out.println("str1 == str3 的结果:" + (str1 == str3));//输出
System.out.println("str1 == str4 的结果:" + (str1 == str4));//输出
System.out.println("str2 == str4 的结果:" + (str2 == str4));//输出
System.out.println("str1.equals(str2) 的结果:" + str1.equals(str2));//输出
System.out.println("str1.equals(str3) 的结果:" + str1.equals(str3));//输出
System.out.println("str1.equals(str4) 的结果:" + str1.equals(str4));//输出
}
}
例5.27
public class zifuchuanzj {
public static void main(String[] args) {
// 创建字符串对象,内容全部小写
String str1 = "abc";
// 创建字符串对象,内容全部大写
String str2 = "ABC";
// 比较两个字符串的内容是否相等
System.out.println("区分大小写的结果:"+str1.equals(str2));
// 比较两个字符串的内容是否相等,不区分大小写
System.out.println("不区分大小写的结果:"+str1.equalsIgnoreCase(str2));//输出
}
}
例5.28
Date date = new Date(); //创建Date对象date
String year = String.format("%tY", date); //将date进行格式化
String month = String.format("%tB", date);
String day = String.format("%td", date);
System.out.println("今年是:" + year + "年"); //输出信息
System.out.println("现在是:" + month);
System.out.println("今天是:" + day + "号");
}
}
例5.29
import java.util.Date; //导入java.util.Date类
public class zifuchuanzj {
public static void main(String[] args) {
Date date = new Date(); // 创建Date对象date
String hour = String.format("%tH", date); // 将date进行格式化
String minute = String.format("%tM", date);//输出
String second = String.format("%tS", date);
// 输出的信息
System.out.println("现在是:" + hour + "时" + minute + "分" + second + "秒");//输出
}
}
例5.30
import java.util.Date; //导入java.util.Date类
public class zifuchuanzj {
public static void main(String[] args) {
Date date = new Date(); // 创建Date对象date
String time = String.format("%tc", date); // 将date格式化
String form = String.format("%tF", date);
// 将格式化后的日期时间输出
System.out.println("全部的时间信息是:" + time);
System.out.println("年-月-日格式:" + form);
}
}
例5.31
// 输出字符
String str1 = String.format("%c", 'X');
System.out.println("字母x大写:" + str1);
// 输出数字
String str2 = String.format("%d", 1251 + 3950);
System.out.println("1251+3950的结果是:" + str2);
// 输出小数点后两位
String str3 = String.format("%.2f", Math.PI);
System.out.println("π取两位小数点:" + str3);
// 输出布尔值
String str4 = String.format("%b", 2 < 3);
System.out.println("2<3的结果是:" + str4);
// 输出哈希散列码,等同Integer.toHexString(3015);
String str5 = String.format("%h", 3510);
System.out.println("3510的hashCode值:" + str5);
// 输出8进制
String str6 = String.format("%o", 33);
System.out.println("33的8进制结果是:" + str6);
// 输出16进制
String str7 = String.format("%x", 33);
System.out.println("33的16进制结果是:" + str7);
// 输出科学计数法
String str8 = String.format("%e", 120000.1);
System.out.println("120000.1用科学计数法表示:" + str8);
// 输出带有效位数和指数的16进制浮点值
String str9 = String.format("%a", 40.0);
System.out.println("40.0的十六进制浮点值:" + str9);
// 输出百分号和数字
System.out.println(String.format("天才是由%d%%的灵感,%d%%的汗水 。", 1, 99));
}
}
例5.32
public class zifuchuanzj {
public static void main(String[] args) {
// 让字符串输出的最大长度为5,不足长度在前端补空格
String str1 = String.format("%5d", 123);
System.out.println("输出长度为5的字符串|" + str1 + "|");
// 让字符串左对齐
String str2 = String.format("%-5d", 123);
System.out.println("左对齐|" + str2 + "|");
// 在8进制前加一个0
String str3 = String.format("%#o", 33);
System.out.println("33的8进制结果是:" + str3);
// 在16进前加一个0x
String str4 = String.format("%#x", 33);
System.out.println("33的16进制结果是:" + str4);
// 显示数字正负号
String str5 = String.format("%+d", 1);
System.out.println("我是正数:" + str5);
// 显示数字正负号
String str6 = String.format("%+d", -1);
System.out.println("我是负数:" + str6);
// 在正数前补一个空格
String str7 = String.format("% d", 1);
System.out.println("我是正数,前面有空格" + str7);
// 在负数前补一个负号
String str8 = String.format("% d", -1);
System.out.println("我是负数,前面有负号" + str8);
// 让字符串输出的最大长度为5,不足长度在前端补0
String str9 = String.format("%05d", 12);
System.out.println("前面不够的数用0填充:" + str9);
// 用逗号分隔数字
String str10 = String.format("%,d", 123456789);
System.out.println("用逗号分隔:" + str10);
// 正数无影响
String str11 = String.format("%(d", 13);
System.out.println("我是正数,我没有括号:" + str11);
// 让负数用括号括起来
String str12 = String.format("%(d", -13);
System.out.println("我是负数,我有括号的:" + str12);
}
}
5.5可变字符串
5.5.1 StringBuffer类的常用方法
例5.33
public class zifuchuanzj {
public static void main(String[] args) {
// 创建StringBuffer对象
StringBuffer sbf = new StringBuffer("门前大桥下,");
// 追加字符串常量
sbf.append("游过一群鸭,");
// 追加StringBuffer对象
StringBuffer tmp = new StringBuffer("快来快来数一数,");
sbf.append(tmp);
// 追加整型变量
int x = 24678;
sbf.append(x);
// 输出
System.out.println(sbf.toString());
}
}
例5.34
public class zifuchuanzj {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("0123456");
System.out.println("sbf的原值是:" + sbf);
sbf.setCharAt(3, 'A'); // 将索引为3的字符改成'A'
System.out.println("修改后的值是:" + sbf);//输出
}
}
例5.35
public class zifuchuanzj {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("0123456");
System.out.println("sbf的原值为:"+sbf);
sbf = sbf.insert(5, "F"); //在索引为5的位置插入"F",将返回值赋给sbf自己
System.out.println("修改之后的值为:"+sbf);//输出
}
}
例5.36
public class zifuchuanzj {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("同一个世界,同一个梦想");
System.out.println("sbf的原值为:" + sbf);
sbf = sbf.reverse();// 将字符序列sbf反转
System.out.println("修改之后的值为:" + sbf);//输出
}
}
例5.37
public class zifuchuanzj {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("天行健,君子以自强不息");
System.out.println("sbf的原值为:"+sbf);
sbf = sbf.delete(4, 7); //删除从索引4开始至索引7之前的内容
System.out.println("删除之后的值为:"+sbf);//输出
}
}
例5.38
public class zifuchuanzj {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("ABCDEFG");// 创建字符串序列
int lenght = sbf.length();// 获取字符串序列的长度
char chr = sbf.charAt(5); // 获取索引为5的字符
int index = sbf.indexOf("DEF");// 获取DEF字符串所在的索引位置
String substr = sbf.substring(0, 2);// 截取从索引0开始至索引2之间的字符串
StringBuffer tmp = sbf.replace(2, 5, "1234");// 将从索引2开始至索引5之间的字符序列替换成"1234"
System.out.println("sbf的原值为:" + sbf);//输出
System.out.println("sbf的长度为:" + lenght);//输出
System.out.println("索引为5的字符为:" + chr);//输出
System.out.println("DEF字符串的索引位置为:" + index);//输出
System.out.println("索引0开始至索引2之间的字符串:" + substr);//输出
System.out.println("替换后的字符串为:" + tmp);//输出
}
}
例5.39
public class zifuchuanzj {
public static void main(String[] args) {
StringBuilder sbd = new StringBuilder();
System.out.println("sbd的原值为空");//输出
sbd.append("我是StringBuilder类");//获取方法
System.out.println("sbd追加字符串:" + sbd);//输出
int length = sbd.length();//获取方法
System.out.println("sbd的长度为:" + length);//输出
sbd = sbd.insert(length - 1, "123456");//获取方法
System.out.println("插入字符串:" + sbd);//输出
sbd = sbd.delete(sbd.length() - 1, sbd.length());
System.out.println("删除最后一个字:" + sbd);//输出
sbd = sbd.reverse();//获取方法
System.out.println("反序输出:" + sbd);
}
}
例5.40
public class zifuchuanzj {
public static void main(String[] args) { // 主方法
String str = "String";
// String转换成StringBuffer
StringBuffer sbf = new StringBuffer(str);
// String转换成StringBuilder
StringBuilder sbd = new StringBuilder(str);
// StringBuffer转换成String
str = sbf.toString();
// StringBuilder转换成String
str = sbd.toString();
// StringBuffer转换成StringBuilder
StringBuilder bufferToBuilder = new StringBuilder(sbf.toString());
// StringBuilder转换成StringBuffer
StringBuffer builderToBuffer = new StringBuffer(sbd.toString());
}
}
例5.41
public class zifuchuanzj {
public static void main(String[] args) { // 主方法
String str = ""; // 创建空字符串
// 定义对字符串执行操作的起始时间
long starTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) { // 利用for循环执行10000次操作
str = str + i; // 循环追加字符串
}
long endTime = System.currentTimeMillis(); // 定义对字符串操作后的时间
long time = endTime - starTime; // 计算对字符串执行操作的时间
System.out.println("String循环1万次消耗时间:" + time); // 将执行的时间输出
StringBuilder builder = new StringBuilder(""); // 创建字符串生成器
starTime = System.currentTimeMillis(); // 定义操作执行前的时间
for (int j = 0; j < 10000; j++) { // 利用for循环进行操作
builder.append(j); // 循环追加字符
}
endTime = System.currentTimeMillis(); // 定义操作后的时间
time = endTime - starTime; // 追加操作执行的时间
System.out.println("StringBuilder循环1万次消耗时间:" + time); // 将操作时间输出
StringBuilder builder2 = new StringBuilder(""); // 创建字符串生成器
starTime = System.currentTimeMillis(); // 定义操作执行前的时间
for (int j = 0; j < 50000; j++) { // 利用for循环进行操作
builder2.append(j); // 循环追加字符
}
endTime = System.currentTimeMillis(); // 定义操作后的时间
time = endTime - starTime; // 追加操作执行的时间
System.out.println("StringBuilder循环5万次消耗时间:" + time); // 将操作时间输出
}
}