溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在java中使用stringbuffer

發(fā)布時(shí)間:2021-04-25 16:12:50 來源:億速云 閱讀:200 作者:Leah 欄目:編程語言

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

Java有哪些集合類

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

1.概念

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

2.特點(diǎn)

  • 長度可變的。

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

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

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

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

從可變性來講String的是不可變的,StringBuilder,StringBuffer的長度是可變的。

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

從線程安全上來StringBuilder是線程不安全的,而StringBuffer是線程安全的。

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

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI