



package com.qc.字符串;import java.util.Arrays;public class Test {public static void main(String[] args) {
// String x="hello";//字符串 char[]
// x = x+"demo";//字符串拼接
// x=x+24+50;
// x=x+true;
// System.out.println(x);//hellodemo2450true
//
// x.replace("o", "li");//o被替換為li
// System.out.println(x);
//
// String[] arr=x.split("");//分割
// System.out.println(Arrays.toString(arr));//[h, e, l, l, o, d, e, m, o, 2, 4, 5, 0, t, r, u, e]
//
// int index=x.indexOf("dem");//返回子串第一個字母的下標
// System.out.println(index);//沒有返回-1
//
// System.out.println(x.substring(2,5));//返回子串,左閉右開
// System.out.println(x.substring(2));//子串下標從2開始
// //java不可變字符串// String s1="hello";//字符串常量池里
// String s2="hello";
// String s3=new String("hello");
// String s4=new String("hello");
// //== 引用類型判斷指向是否相同 基本類型判斷值是否相等
// System.out.println(s1==s2);
// System.out.println(s3==s2);
// System.out.println(s3==s4);
//
// System.out.println(s1.equals(s2));
// System.out.println(s2.equals(s3));
// System.out.println(s3.equals(s4));String s1=null;//null串沒有任何空間String s2="";//空串有指向System.out.println(s1);System.out.println(s2);//API:提供的各種方法}
}
package com.qc.字符串;public class Test2 {public static void main(String[] args) {//String 不可變字符串,每次拼接創建新的String對象 與String相比StringBuffer和StringBuilder這兩個的速度要遠遠高于String//StringBuffer 多線程安全 適合多線程 慢(相對StringBuilder)//StringBuilder 多線程不安全 適合單線程 快long start = System.currentTimeMillis();//時間戳 1970.1.1StringBuilder res = new StringBuilder();for(int i=0;i<10000;i++) {res.append("a");//追加}long end=System.currentTimeMillis();System.out.println("消耗時間:"+(end-start));//--------------------------------------long start1 = System.currentTimeMillis();//時間戳 1970.1.1String res1 ="";for(int i=0;i<10000;i++) {res1+="a";}long end1=System.currentTimeMillis();System.out.println("消耗時間:"+(end1-start1));}//文件流 網絡流 借助緩沖區
}