標籤:問題 oid 對象 刪除 city capacity 不可變 ring 傳遞
StringBuffer類
1.概述
StringBuffer是安全執行緒的可變字串
2.StringBuffer和String的區別?
StringBuffer前者長度和內容可變,後者不可變。
如果使用前者做字串的拼接,不會浪費太多的資源。
3.StringBuffer的構造方法
public StringBuffer():無參構造方法
public StringBuffer(int capacity):指定容量的字串緩衝區對象
public StringBuffer(String str):指定字串內容的字串緩衝區對象
4.StringBuffer常用方法
(1).長度方法(或者是容量方法)
public int capacity():返回當前的容量。理論值
public int length(): 返回長度(字元數)。實際值
使用舉例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println("---"+sb);
System.out.println("sb.capacity():"+sb.capacity());
System.out.println("sb.length():"+sb.length());
System.out.println("-----------------------");
StringBuffer sb1=new StringBuffer("helloworld");
System.out.println("---"+sb1);
System.out.println("sb.capacity():"+sb1.capacity());
System.out.println("sb.length():"+sb1.length());
}
//輸出結果:
---
sb.capacity():16
sb.length():0
-----------------------
---helloworld
sb.capacity():26(16+10)
sb.length():10
(2).添加方法
public StringBuffer append(String str):可以把任意類型的資料添加到字串緩衝區裡面,並返回緩衝區本身。
public StringBuffer insert(int offset,String str):在指定的位置把任意類型的資料插入字串緩衝區裡面,並返回緩衝區本身。
使用舉例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
sb.append("你好").append("世界").append("---").append("hello").append("world");
System.out.println(sb);
System.out.println("--------------");
sb.insert(2,"test");
System.out.println(sb);
}
//輸出結果
你好世界---helloworld
--------------
你好test世界---helloworld
(3).刪除功能
public StringBuffer deleteChartAt(int index):刪除指定位置的字元,並返回刪除以後的字串緩衝區。
public StringBuffer delete(int start,int end):從指定位置開始到指定位置結束刪除,返回字串本身。
使用舉例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
sb.deleteCharAt(2);
System.out.println(sb);
System.out.println("--------------");
sb.delete(0, 3);
System.out.println(sb);
}
//輸出結果
heloworld
--------------
oworld
(4).替換功能
public StringBuffer replace(int strat,int end,String str):從指定位置start開始到end使用str字串來替換,包左不包右。
使用舉例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
String str="123";
sb.replace(1, 3, str);
System.out.println(sb);
}
//輸出結果
h123loworld
(5).反轉功能
public StringBuffer reverse():把原有的字串倒著輸出
使用舉例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
System.out.println(sb);
System.out.println("------------------------");
sb.reverse();
System.out.println(sb);
}
//輸出結果
helloworld
------------------------
dlrowolleh
(6).StringBuffer的截取功能(不再是返回本身,而是返回截取的內容)
public String substring(int start);從指定位置開始截取,把截取的字串重新賦值給一個字串
public String substring(int start,int end);從指定位置開始到最後截取,把截取的字串重新賦值給一個字串
使用舉例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
String str=sb.substring(3);
System.out.println(str);
System.out.println("----------------------");
String str1=sb.substring(1,3);
System.out.println(str1);
}
//輸出結果
loworld
----------------------
el
5.StringBuffer的相關操作
(1).String和StringBuffer之間的相互轉化
為什麼我們要講解類之間的轉化
String-->StringBuffer
我們可以使用StringBuffer的功能
但是我們需要的還是String類,所以
StringBuffer-->String
代碼:
public static void main(String[] args) {
String str ="helloworld";
//String-->StringBuffer
//通過構造
StringBuffer sb =new StringBuffer(str);
//通過apends()方法
StringBuffer sb1=new StringBuffer();
sb1.append(str);
System.out.println("sb:"+sb);
System.out.println("sb1:"+sb1);
System.out.println("-----------------");
//StringBuffer--->String
StringBuffer buffer =new StringBuffer("java");
//通過String構造
String str1=new String(buffer);
//通過String的toString()方法
String str2=buffer.toString();
System.out.println("str1:"+str1);
System.out.println("str2:"+str2);
}
//輸出結果
sb:helloworld
sb1:helloworld
-----------------
str1:java
str2:java
6.相關面試題
(1).String,StringBuffer,StringBuilder的區別
String的內容是不可變的,而StringBuffer,StringBuilder的內容可變
StringBuffer是同步的,資料是安全的,效率低;StringBilder是不同步的,資料不安全,效率高。
(2).StringBuffer和數組的區別?
StringBuffer和數組都可以看成是一個容器,用於轉載資料
但是呢,StringBuffer的資料最終是一個字串資料(資料類型可以是不一樣的)。
而數組可以放置多個資料,但是必須是同一種資料類型。
(3).形式參數問題(*)
String作為參數傳遞
StringBuffer作為參數傳遞
形式參數:
基本類型:形式參數的改變不會影響實際參數
參考型別:形式參數的改變會直接影響實際參數
Java API_StringBuffer類