詳解Java中StringBuffer類常用方法
String是不變類,用String修改字符串會(huì)新建一個(gè)String對(duì)象,如果頻繁的修改,將會(huì)產(chǎn)生很多的String對(duì)象,開銷很大.因此java提供了一個(gè)StringBuffer類,這個(gè)類在修改字符串方面的效率比String高了很多。
在java中有3個(gè)類來負(fù)責(zé)字符的操作。
- 1.Character 是進(jìn)行單個(gè)字符操作的,
- 2.String 對(duì)一串字符進(jìn)行操作,不可變類。
- 3.StringBuffer 也是對(duì)一串字符進(jìn)行操作,但是可變類。
public class UsingStringBuffer { /** * 查找匹配字符串 */ public static void testFindStr() { StringBuffer sb = new StringBuffer(); sb.append("This is a StringBuffer"); // 返回子字符串在字符串中最先出現(xiàn)的位置,如果不存在,返回負(fù)數(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)的位置,如果不存在,返回負(fù)數(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"); // 默認(rèn)的終止位置為字符串的末尾 System.out.print("sb.substring(4)=" + sb.substring(4)); // substring方法截取字符串,可以指定截取的起始位置和終止位置 System.out.print("sb.substring(4,9)=" + sb.substring(4, 9)); } /** * 獲取字符串中某個(gè)位置的字符 */ 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!"); // 將字符串中某段字符替換成另一個(gè)字符串 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)容時(shí),不會(huì)創(chuàng)建新的對(duì)象,因此,它比String類更適合修改字符串;
StringBuffer類沒有提供同String一樣的toCharArray方法;
StringBuffer類的replace方法同String類的replace方法不同,它的replace方法有三個(gè)參數(shù),第一個(gè)參數(shù)指定被替換子串的起始位置,第二個(gè)參數(shù)指定被替換子串的終止位置,第三個(gè)參數(shù)指定新子串。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解
這篇文章主要介紹了SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解,事務(wù)管理對(duì)于企業(yè)應(yīng)用來說是至關(guān)重要的,當(dāng)出現(xiàn)異常情況時(shí),它也可以保證數(shù)據(jù)的一致性,需要的朋友可以參考下2023-08-08springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java FileInputStream讀中文亂碼問題解決方案
這篇文章主要介紹了Java FileInputStream讀中文亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java在排序數(shù)組中查找元素的第一個(gè)和最后一個(gè)位置的方法詳解
相信大家在操作Java的時(shí)候經(jīng)常會(huì)要在一個(gè)數(shù)組(無序)中查找元素的第一個(gè)和最后一個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于Java在排序數(shù)組中查找元素的第一個(gè)和最后一個(gè)位置的相關(guān)資料,需要的朋友可以參考下2024-01-01Java通過Lambda表達(dá)式實(shí)現(xiàn)簡(jiǎn)化代碼
我們?cè)诰帉懘a時(shí),常常會(huì)遇到代碼又長(zhǎng)又重復(fù)的情況,就像調(diào)用第3方服務(wù)時(shí),每個(gè)方法都差不多,?寫起來啰嗦,?改起來麻煩,?還容易改漏,所以本文就來用Lambda表達(dá)式簡(jiǎn)化一下代碼,希望對(duì)大家有所幫助2023-05-05SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例,包含gRPC的工作原理,以及如何在Spring Boot應(yīng)用中集成gRPC,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02