如何在java中使用stringbuffer

本篇文章給大家分享的是有關(guān)如何在java中使用stringbuffer,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),來(lái)賓企業(yè)網(wǎng)站建設(shè),來(lái)賓品牌網(wǎng)站建設(shè),網(wǎng)站定制,來(lái)賓網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,來(lái)賓網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

Java有哪些集合類(lèi)

Java中的集合主要分為四類(lèi):1、List列表:有序的,可重復(fù)的;2、Queue隊(duì)列:有序,可重復(fù)的;3、Set集合:不可重復(fù);4、Map映射:無(wú)序,鍵唯一,值不唯一。

1.概念

StringBuffer又稱(chēng)為可變字符序列,它是一個(gè)類(lèi)似于 String 的字符串緩沖區(qū),通過(guò)某些方法調(diào)用可以改變?cè)撔蛄械拈L(zhǎng)度和內(nèi)容。原來(lái)StringBuffer是個(gè)字符串的緩沖區(qū),即就是它是一個(gè)容器,容器中可以裝很多字符串。并且能夠?qū)ζ渲械淖址M(jìn)行各種操作。

2.特點(diǎn)

  • 長(zhǎng)度可變的。

  • 可以存儲(chǔ)不同類(lèi)型數(shù)據(jù)。

  • 最終要轉(zhuǎn)成字符串進(jìn)行使用。

  • 可以對(duì)字符串進(jìn)行修改。

3.String、StringBuilder、StringBuffer的區(qū)別

從可變性來(lái)講String的是不可變的,StringBuilder,StringBuffer的長(zhǎng)度是可變的。

從運(yùn)行速度上來(lái)講StringBuilder > StringBuffer > String。

從線(xiàn)程安全上來(lái)StringBuilder是線(xiàn)程不安全的,而StringBuffer是線(xiàn)程安全的。

4.實(shí)例

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));
}
 
/**
 * 添加各種類(lèi)型的數(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());
}
 
/**
 * 向字符串中插入各種類(lèi)型的數(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方法將字符串倒序
}
}

以上就是如何在java中使用stringbuffer,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)標(biāo)題:如何在java中使用stringbuffer
URL分享:http://muchs.cn/article24/iidoce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、微信公眾號(hào)、品牌網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站