詳解Java中StringBuffer類常用方法
String是不變類,用String修改字符串會新建一個String對象,如果頻繁的修改,將會產(chǎn)生很多的String對象,開銷很大.因此java提供了一個StringBuffer類,這個類在修改字符串方面的效率比String高了很多。
在java中有3個類來負責字符的操作。
- 1.Character 是進行單個字符操作的,
- 2.String 對一串字符進行操作,不可變類。
- 3.StringBuffer 也是對一串字符進行操作,但是可變類。
public class UsingStringBuffer {
/**
* 查找匹配字符串
*/
public static void testFindStr() {
StringBuffer sb = new StringBuffer();
sb.append("This is a StringBuffer");
// 返回子字符串在字符串中最先出現(xiàn)的位置,如果不存在,返回負數(shù)
System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));
// 給indexOf方法設(shè)置參數(shù),指定匹配的起始位置
System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));
// 返回子字符串在字符串中最后出現(xiàn)的位置,如果不存在,返回負數(shù)
System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));
// 給lastIndexOf方法設(shè)置參數(shù),指定匹配的結(jié)束位置
System.out.println("sb.lastIndexOf(\"is\", 1) = "
+ sb.lastIndexOf("is", 1));
}
/**
* 截取字符串
*/
public static void testSubStr() {
StringBuffer sb = new StringBuffer();
sb.append("This is a StringBuffer");
// 默認的終止位置為字符串的末尾
System.out.print("sb.substring(4)=" + sb.substring(4));
// substring方法截取字符串,可以指定截取的起始位置和終止位置
System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
}
/**
* 獲取字符串中某個位置的字符
*/
public static void testCharAtStr() {
StringBuffer sb = new StringBuffer("This is a StringBuffer");
System.out.println(sb.charAt(sb.length() - 1));
}
/**
* 添加各種類型的數(shù)據(jù)到字符串的尾部
*/
public static void testAppend() {
StringBuffer sb = new StringBuffer("This is a StringBuffer!");
sb.append(1.23f);
System.out.println(sb.toString());
}
/**
* 刪除字符串中的數(shù)據(jù)
*/
public static void testDelete() {
StringBuffer sb = new StringBuffer("This is a StringBuffer!");
sb.delete(0, 5);
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
/**
* 向字符串中插入各種類型的數(shù)據(jù)
*/
public static void testInsert() {
StringBuffer sb = new StringBuffer("This is a StringBuffer!");
// 能夠在指定位置插入字符、字符數(shù)組、字符串以及各種數(shù)字和布爾值
sb.insert(2, 'W');
sb.insert(3, new char[] { 'A', 'B', 'C' });
sb.insert(8, "abc");
sb.insert(2, 3);
sb.insert(3, 2.3f);
sb.insert(6, 3.75d);
sb.insert(5, 9843L);
sb.insert(2, true);
System.out.println("testInsert: " + sb.toString());
}
/**
* 替換字符串中的某些字符
*/
public static void testReplace() {
StringBuffer sb = new StringBuffer("This is a StringBuffer!");
// 將字符串中某段字符替換成另一個字符串
sb.replace(10, sb.length(), "Integer");
System.out.println("testReplace: " + sb.toString());
}
/**
* 將字符串倒序
*/
public static void reverseStr() {
StringBuffer sb = new StringBuffer("This is a StringBuffer!");
System.out.println(sb.reverse()); // reverse方法將字符串倒序
}
}
小結(jié):
StringBuffer不是不變類,在修改字符串內(nèi)容時,不會創(chuàng)建新的對象,因此,它比String類更適合修改字符串;
StringBuffer類沒有提供同String一樣的toCharArray方法;
StringBuffer類的replace方法同String類的replace方法不同,它的replace方法有三個參數(shù),第一個參數(shù)指定被替換子串的起始位置,第二個參數(shù)指定被替換子串的終止位置,第三個參數(shù)指定新子串。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關(guān)文章
spring boot+vue 的前后端分離與合并方案實例詳解
這篇文章主要介紹了spring boot+vue 的前后端分離與合并方案實例詳解,需要的朋友可以參考下2017-11-11
org.apache.ibatis.annotations不存在的問題
這篇文章主要介紹了org.apache.ibatis.annotations不存在的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
JSP 開發(fā)之hibernate的hql查詢多對多查詢
這篇文章主要介紹了JSP 開發(fā)之hibernate的hql查詢多對多查詢的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Java實現(xiàn)ATM銀行管理系統(tǒng)(控制臺版本)
這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)控制臺版本的ATM銀行管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
Java語法基礎(chǔ)之選擇結(jié)構(gòu)的if語句、switch語句詳解
這篇文章主要為大詳細介紹了Java語法基礎(chǔ)之選擇結(jié)構(gòu)的if語句、switch語句,感興趣的小伙伴們可以參考一下2016-09-09
Java從數(shù)據(jù)庫中讀取Blob對象圖片并顯示的方法
這篇文章主要介紹了Java從數(shù)據(jù)庫中讀取Blob對象圖片并顯示的方法,實例分析了Java讀取數(shù)據(jù)庫中Blob對象圖片的技巧與操作方法,需要的朋友可以參考下2015-02-02

