chapter 6 之字符串

package chapter6;
public class StringClass{
	//1.String类的重要概念是:一旦创建了一个String对象,就永远不能改变它
	public void stringUnique(){
		String s ="abc";
		s.concat("def");
		System.out.println("test 1:"+s);//test 1 you want to display "abcdef"
		s.toUpperCase();
		System.out.println("test 2:"+s);//test 2 you want to display "ABCDEF"
		s=s.toUpperCase();
		System.out.println("test 3:"+s);//test 3 you want to display "ABC"
		//outPut is:
		//test 1:abc
		//test 2:abc
		//test 3:ABC
	}
	
	//2.String易错方法辨析
	public void stringMethod(){
		String s ="wo ai Liu Haha 10000 nian~";
		System.out.println("charAt():"+s.charAt(1));
		System.out.println("replace():"+s.replace('o', 'e'));//replace的参数是字符型
		System.out.println("subString():"+s.substring(3,5));//第一个参数是从0开始计数,第二个是从1开始计数,包括数到的那个数
		//数组有length属性,String有length方法
		String[] ss =new String[5];//注意数组名后的方括号里是数组的大小!
		System.out.println("ss.length:"+ss.length+"  "+"s.length()"+s.length());
		//outPut is:
		//charAt():o
		//replace():we ai Liu Haha 10000 nian~
		//subString():ai
		//ss.length:5  s.length()26
	}
	
	//StringBuilder是更快、非同步(不是线程安全的)的StringBuffer,它们两个有完全相同的方法
	public void builderAndBuffer(){
		StringBuffer sb =new StringBuffer("wo ai liu haha");
		sb.append(" ");sb.append(10000);sb.append(" nian");//append()方法可带多种形式的变元
		System.out.println("append():"+sb);
		sb.delete(3,5);
		System.out.println("delete():"+sb);
		sb.insert(3,"ai");//insert()方法第一个参数(int)偏移量,第二个参数(String)插入内容
		System.out.println("insert();"+sb);
		sb.reverse();
		System.out.println("reverse():"+sb);
		//outPut is
		//append():wo ai liu haha 10000 nian
		//delete():wo  liu haha 10000 nian
		//insert();wo ai liu haha 10000 nian
		//reverse():nain 00001 ahah uil ia ow
	}
	
	
	public static void main(String[] args){
		StringClass sc =new StringClass();
		//sc.stringUnique();
		//sc.stringMethod();
		//sc.builderAndBuffer();
	}
}

你可能感兴趣的:(字符串)