二、StringBuffer類的常用方法| No. | 方法定義 | 類型 | 描述 |
| 1 | public StringBuffer() | 構造 | StringBuffer的構造方法 |
| 2 | public StringBuffer append(char c) | 方法 | 在StringBuffer中提供了大量的追加操作(與String中使用“+”類似),可以向StringBuffer中追加內容,此方法可以添加任何的數據類型。 |
| 3 | public StringBuffer append(String str) | 方法 | |
| 4 | public StringBuffer append(StringBuffer sb) | 方法 | |
| 5 | public int indexOf(String str) | 方法 | 查找指定字符串是否存在 |
| 6 | public int indexOf(String str,int fromIndex) | 方法 | 從指定位置開始查找指定字符串是否存在 |
| 7 | public StringBuffer insert(int offset,String str) | 方法 | 在指定位置處加上指定字符串 |
| 8 | public StringBuffer reverse() | 方法 | 將內容反轉保存 |
| 9 | public StringBuffer replace(int start,int end,String str) | 方法 | 指定內容替換 |
| 10 | public int length() | 方法 | 求出內容長度 |
| 11 | public StringBuffer delete(int start,int end) | 方法 | 刪除指定范圍的字符串 |
| 12 | public String substring(int start) | 方法 | 字符串截取,指定開始點 |
| 13 | public String substring(int start,int end) | 方法 | 截取指定范圍的字符串 |
| 14 | public String toString() | 方法 | Object類繼承的方法,用于將內容變為String類型 |
package com.pb.demo1;public class StringTest { public static void main(String[] args) { String str="要好好學習Java!"; //長度length()方法 System.out.QQ.com"; String [] emaillist=email.split(";"); //遍歷數組 for (String s : emaillist) { System.out.println(s); } }}四、StringBuffer類package com.pb.demo1;public class StringBufferTest { public static void main(String[] args) { StringBuffer str=new StringBuffer("abc_def_add_zzD"); //查找_第一次出現的位置 int indexfirst=str.indexOf("_"); System.out.println("查找_第一次出現的位置"+indexfirst); //在第一個"_"的后面加上"A" str.insert(indexfirst+1, "A"); System.out.println(str); //查找最后一次出現"_"的索引位置 int indexlast=str.lastIndexOf("_"); System.out.println("查找最后一次出現_的索引位置"+indexlast); //刪除最后一個"_" //str.deleteCharAt(indexlast); str.delete(indexlast, indexlast+1); System.out.println(str); System.out.println("==================="); StringBuffer email=new StringBuffer("rock105@sohu.com"); ////查找"@"的索引位置 int index=email.indexOf("@"); email.replace(0, index, "tom"); System.out.println(email); }}新聞熱點
疑難解答