標籤:
String StringBuffer StringBuilder
String http://docs.oracle.com/javase/7/docs/api/
中文: http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html
String str0 = "最正常的建立方法"; System.out.println(str0); char[] c1 = new char[3]; c1[0] = ‘c‘; c1[1] = ‘b‘; c1[2] = ‘a‘; String str1 =new String("string1"); String str2 = new String(c1); System.out.println(str1+ " "+ str2); //輸出string1 cba /* String.valueOf()它可以將JAVA基本類型(int,double,boolean等)和對象(Object)轉換成String型 * toString()是對象的方法,它可以將該對象轉換成String型,轉換演算法根據類型實際需要而定, * 基本上JAVA裡面每個對象都會有一個toString方法。*/ //字串轉換 int i1 = 100; String str3= Integer.toString(i1); System.out.println("Integer.toString : "+str3);//輸出Integer.toString : 100 //valueOf()進行資料轉換 String str32 = String.valueOf(123456); System.out.println("String.valueOf() : " +str32);//輸出String.valueOf() : 123456 //字元抽取 //charAt String str4 = "A long String!"; System.out.println("charAt(3) : "+str4.charAt(3));//輸出charAt(3) : o //getChars() char buf [] = new char[6-2]; str4.getChars(2, 6, buf , 0); System.out.print("getChars輸出 :"); System.out.println(buf); //輸出getChars輸出 :long //char[] toCharArray() System.out.println(str4.toCharArray());//輸出A long String! //抽取子串 String str5 = str4.substring(2,8); System.out.println("subString(2,8)輸出 : "+str5);//輸出subString(2,8)輸出 : long S
StringBuffer http://docs.oracle.com/javase/7/docs/api/
/* * StringBuffer() * StringBuffer(int size) * StringBuffer(String str) * 接收一個String變元,指定StringBuffer對象的初始內容,並額外預留16個字元空間 * StringBuffer(CharSequence chars) */ StringBuffer sbf = new StringBuffer("Hello"); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity()); System.out.println("----重新指定緩衝區大小----"); sbf.ensureCapacity(55); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity()); System.out.println("----設定緩衝區長度----"); sbf.setLength(30); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity());
輸出結果:
buffer = Hellolength = 5capacity = 21----重新指定緩衝區大小----buffer = Hellolength = 5capacity = 55----設定緩衝區長度----buffer = Hello
StringBuilder http://docs.oracle.com/javase/7/docs/api/
除了一個重要的區別之外,StringBuffer等同於StringBuilder,區別是:它不是同步的,即意味著它不是安全執行緒的。
StringBuilder優勢在於具有更快的效能,但是使用多線程時必須使用StringBuffer,而不能使用StringBuilder
Java學習筆記--String StringBuffer StringBuilder