/**
* @Description StringBuffer和 StringBuilder
*
* StringBuffer和 StringBuilder的使用和 String区别
* (1)效率比 String高 (2)比 String节省空间
*
* @author AI福
*
*/
package com.chapter.demo06;
public class Demo5 {
public static void main(String[] args) {
// StringBuffer s1 = new StringBuffer();
StringBuilder s1 = new StringBuilder();
//1. append()追加
s1.append("java");
System.out.println(s1.toString());
s1.append("是面向对象编程的语言");
System.out.println(s1.toString());
s1.append("java 真不错!");
System.out.println(s1.toString());
//2.insert(); 添加
s1.insert(0,"我在最前面");
System.out.println(s1.toString());
//3.replace(); 指定位置进行替换
s1.replace(0,5,"hello");
System.out.println(s1.toString());
//4.delete(); 删除
s1.delete(0,5);
System.out.println(s1.toString());
//清空
s1.delete(0,s1.length());
System.out.println(s1.length());
}
}
/**
* @Description StringBuffer和 StringBuilder
*
* 验证 StringBuffer效率高于 String
* @author AI福
*/
package com.chapter.demo06;
public class Demo06 {
public static void main(String[] args) {
//开始时间
long start = System.currentTimeMillis();
/**
String string = "";
for (int i = 0; i < 99999; i++) {
string += i;
}
System.out.println(string);
*/
StringBuilder s1 = new StringBuilder();
for (int i = 0; i < 99999; i++) {
s1.append(i);
}
System.out.println(s1.toString());
//结束时间
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));
}
}
//Ctrl + A 全选
//Ctrl + alt + L 自动对齐