1. 程式人生 > >Java String API詳解

Java String API詳解

public class StringClass {
	
	public static void main(String[] args) {
		String str = new String("I am a lucky string."); //構造器
		System.out.println("My length is " + str.length()); //length()
		System.out.println("My favoriate character is " + str.charAt(8));  //charAt() Output:u
		char dst[] = {'a', 'p', 'o', 'o', 'r', 'g', 'i', 'r', 'l'};
		System.out.println("Now I want to pass my lucky to a good guy");
		str.getChars(7, 12, dst, 0);    //getChars(),Output:luckygirl
		System.out.println(dst);
		byte[] b_gbk = str.getBytes();  //getBytes()
		System.out.println(b_gbk);
		dst = str.toCharArray(); //toCharArray()
		System.out.println(dst);   //output:I am a lucky string.
		
		if (str.equals("I am a unlucky string.")) {   //equals()
			System.out.println("The same");
		} else {
			System.out.println("Diffenent");
		}
		
		if (str.equalsIgnoreCase("I AM A LUCKY STRING.")) {   //equalsIgnoreCase()
			System.out.println("The same");
		} else {
			System.out.println("Diffenent");
		}
		
		if (str.compareTo("I am a unlucky string.") > 0) {   //compareTo(),Output:I am smaller
			System.out.println("I am bigger");
		} else {
			System.out.println("I am smaller");
		}
		
		if (str.contains("lucky")) {                             //contains()
			System.out.println("I contain lucky word");
		} else {
			System.out.println("I don't contain lucky word");
		}
		
		StringBuffer strBuf = new StringBuffer("I am a lucky string.");
		if (str.contentEquals(strBuf)) {                             //contentEquals(),Output:The same
			System.out.println("The same");
		} else {
			System.out.println("Diffenent");
		}
		
		String strNew = new String("I AM A LUCKY STRING.");
		if (str.regionMatches(true, 7, strNew, 7, 5)) {                             //regionMatches()
			System.out.println("The same");
		} else {
			System.out.println("Diffenent");
		}
		
		if (str.startsWith("I")) {                             //startsWith()
			System.out.println("I start with I.");
		} else {
			System.out.println("I don't start with I.");
		}
		
		if (str.endsWith("string.")) {                             //endsWith()
			System.out.println("I end with string.");
		} else {
			System.out.println("I don't end with string.");
		}
		
		System.out.println(str.indexOf(97));          //indexOf()        
		System.out.println(str.lastIndexOf(97));         //lastIndexOf()
		System.out.println(str.substring(7, 11));        //substring,輸出:	luck	
		System.out.println(str.concat(" Do you like me? "));        //concat,輸出:I am a lucky string. Do you like me?	
		System.out.println(str.replace('a', 'A'));        //replace,輸出:I Am A lucky string.
		System.out.println(str.toUpperCase());            //toUpperCase
		strNew = "          I am a lucky string.            ";
		System.out.println(strNew.trim());        //trim,輸出:I am a lucky string.
		System.out.println(str.valueOf(8.8));      //valueOf輸出:8.8
		
		
		String str1 = "a"; 
		String str2 = "bc"; 
		String str3 = "a"+"bc"; 
		String str4 = str1+str2; 

		System.out.println(str3 == str4);         //輸出:false
		str4 = (str1 + str2).intern();            //重點:intern()
		System.out.println(str3 == str4);         //輸出:true
	}
}

如有不足之處,歡迎指出。