Java基礎筆記-String類2

來源:互聯網
上載者:User

標籤:

StringBuffer

特點:

  1. 是字串緩衝區.
  2. 是一個容器,其長度可變,可以操作添加多個資料類型.
  3. 最後通過toString方法變成字串.

被final鎖修飾,因此不能被繼承.

 

儲存:

方法1:append() 添加資料到緩衝區.

傳回型別:StringBuffer

方法: append(指定資料)  將指定資料添加到已有資料的結尾.

 

方法2:insert(index,資料內容)

傳回型別:StringBuffer

方法: insert(index,資料內容) 將資料內容插入到指定的index位置中去.

 

刪除:

方法1: delete(int start,int end) 刪除緩衝區的資料.

傳回型別:StringBuffer

方法: delete(int start,int end) 將資料從start開始到end的位置刪除

包含頭不包含尾部,即包含start的位置,不包含end的位置.

方法2: deletecharAt(index)  刪除指定index位置的字元

 

替換:

方法1: replace(位置1,位置2,(String類型的)替換內容)

傳回型別為StringBuffer

方法: replace(位置1,位置2,(String類型的)替換內容)

將資料從位置1到位置2(不包含位置2)的資料替換成要要替換的字串內容.

方法2: setCharAt(位置,要替換的內容)

傳回型別 void

方法: setCharAt(int位置,char要替換的內容) 將資料從指定的位置用替換的字元內容.

 

反轉: reverse() 傳回型別StringBuffer.

有關的代碼執行個體:

 1 class StringBufferDemo 2 { 3     public static void main(String args[]) 4     { 5         //function_add(); 6         //function_del(); 7         StringBuffer sb = new StringBuffer("abcdefg"); 8         char [] ch = new char[6]; 9         sb.getChars(1,4,ch,1);10         for(int i = 0; i <=ch.length; i++)11         {12             printer("ch ["+i+"]="+ch[i]+";");13         }14     }15     public static void function_del()16     {17         StringBuffer sb = new StringBuffer("abcdefg");18         19         // sb.delete(0,sb.length()); //清空緩衝區.20         21         sb.delete(2,5); //刪除緩衝區中字串位置2到5的資料,不包含位置5.22         printer(sb.toString());23         // sb.delete(1,2);24         sb.deleteCharAt(1); //刪除一個資料,等價於上面的代碼: sb.delete(1,2);25         26         printer(sb.toString());27         28     }29     public static void function_add()30     {31         StringBuffer sb = new StringBuffer();32         sb.append("abc").append("def").append(true).append(123);33         34         //StringBuffer sb1 = sb.append(34);35         36         sb.insert(1,"QQQ"); //將字串"QQQ"從位置1中插入到緩衝區的資料中.37         38         // sb.insert(100,"QQQ"); 編譯的時候會出現角標越界的異常.39         //printer("sb==sb1?:"+(sb==sb1));40         41         printer(sb.toString());42         //printer(sb1.toString());43     }44         45     public static void printer(String str)46     {47         System.out.println(str);48     }49 }

 

JDK1.5版本以後出現了StringBuilder

StringBuffer是線程同步.

StringBuidlder是線程不同步,單線程的時候效率高.快捷.

以後開發建議使用StringBuilder.

JDK升級一般三個方面:

  1. 1.  提高效率
  2. 2.  提高安全性
  3. 簡化書寫

 

基礎資料類型對象封裝類:

小寫是基礎資料型別 (Elementary Data Type)  大寫開頭是類

int    Integer

byte   Byte

float   Float

double   Double

boolean   Boolean

char       Character

short    Short

long    Long

 

1.擷取整型變數的最大值: Integer.MAX_VALUE

例如:printer("int max = "+Integer.MAX_VALUE);

 

最常見應用為:

將基礎資料型別 (Elementary Data Type)轉成字串.

    方法1:  基礎資料型別 (Elementary Data Type)+””

    方法2:  基礎資料型別 (Elementary Data Type)類.toString(基礎資料型別 (Elementary Data Type))

    例如: Integer.toString(34); 將整數34轉成字串類型的”34”.

 

將字串轉成基礎資料型別 (Elementary Data Type)

將一個字串轉成整數:

(靜態調用方式)

Integer.parseInt(“32”); 將字串”32”轉成整數 32.

 

格式為:

xxx a = Xxx.parseXxx(String);

例如: int a = Integer.parseInt(“123”);

 

非靜態方法轉換 (對象調用方式)

Integer a = new Integer(“123”);

int num = a.intValue();

 

進位之間轉換:

十進位轉成其他進位:

Integer.toBinayString(要轉的數); //轉成二進位

Integer.toHexString(要轉的數);   //轉成十六進位

Integer.toOctalString(要轉的數); //轉成八進位

其他進位轉成十進位:

格式:

xxx a = Xxx.parseXxx(要轉換的數,要轉的進位)

例如: int a = Integer.parseInt(“110”,2); 將110用二進位轉成十進位數.

括弧中的數和轉換的進位需要相互對應.不然的話編譯會有異常發生.

 相關的簡單代碼執行個體:

 1 class IntegerDemo 2 { 3     public static void main(String args[]) 4     { 5         //擷取整型變數的最大值 6         printer("int max = "+Integer.MAX_VALUE); 7          8         int num = Integer.parseInt("123"); //將字串"123"轉成整數123. 9         num = num +4; //將整數4和整數123先加10         printer("num ="+num);  //輸出的結果為127.11         12         printer(Integer.toBinaryString(-6));//將十進位數轉成位元13         printer(Integer.toHexString(60));        //將十進位數轉成十六進位數14         printer(Integer.toOctalString(60)); //將十進位數轉成八位元15         16         int a = Integer.parseInt("110",2); //將位元110按2進位轉換成十進位數 // 位元110對應的十進位數是6.17         printer("a="+a); //結果a=6.18         19     }20      21     public static void printer(String str)22     {23         System.out.println(str);24     }25         26 27 }
 1 class IntegerDemo1 2 { 3     public static void main(String args[]) 4     { 5         //function(); 6         //Integer a = new Integer(4); 7         Integer a = 4; //自動裝箱 8         a=a+2; //a+2中的a相當於做了a.intValue()操作,a自動拆箱變成了int類型.然後和2進行運算,在將和進行自動的裝箱操作賦值給a. 9         printer("a="+a);10         11         12         Integer x = 128;13         Integer y = 128;14         printer("x==y:?"+(x==y)); //結果是false.15         16         Integer m = 127;17         Integer n = 127;18         printer("m==n:?"+(m==n)); //結果為true. m 和n指向的是同一個Integer對象19                                                             //因為當數值在byte範圍之內的時候,對於JDK1.5以後的新版本,如果該數值已經存在,不會在開闢新的空間.20         21     }22     public static void function()23     {24         Integer x = new Integer("123");25         Integer y = new Integer(123);26         printer("x==y:?"+(x==y));27         printer("x.equals(y):?"+x.equals(y));28     }29     public static void printer(String str)30     {31         System.out.println(str);32     }33     34     35 }

 

Java基礎筆記-String類2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.