
 

 

 

 
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));}//文件流 网络流 借助缓冲区
}