日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能

發布時間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

學習筆記,僅供參考


文章目錄

      • StringBuffer類
      • StringBuffer類的添加功能
        • append方法
        • insert方法
      • 舉個例子



StringBuffer類


StringBuffer類的添加功能


append方法


public StringBuffer append(String str)

Appends the string representation of the Object argument.
The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.

(不是翻譯)該方法可以把任意類型數據添加到字符串緩沖區里面,并返回字符串緩沖區本身,StringBuffer是字符串緩沖區,當new的時候是在堆內存創建了一個對象,底層是一個長度為16的字符數組,當調用append的方法時,不會再重新創建對象,而是不斷向原緩沖區添加字符。

  • 參數

    • str - a string.
  • 返回

    • a reference to this object.


insert方法


public StringBuffer insert(int index,char[] str,int offset,int len)

Inserts the string representation of a subarray of the str array argument into this sequence. The subarray begins at the specified offset and extends len chars. The characters of the subarray are inserted into this sequence at the position indicated by index. The length of this sequence increases by len chars.

  • 參數

    • index - position at which to insert subarray.
    • str - A char array.
    • offset - the index of the first char in subarray to be inserted.
    • len - the number of chars in the subarray to be inserted.
  • 返回:

    • This object

public StringBuffer insert(int offset,String str)

Inserts the string into this character sequence. The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. If str is null, then the four characters “null” are inserted into this sequence.

  • 參數

    • offset - the offset.
    • str - a string.
  • 返回

    • a reference to this object.

舉個例子


  • 例子1
package com.guiyang.restudy3;public class Demo2StringBuffer {public static void main(String[] args) {StringBuffer sb = new StringBuffer();StringBuffer sb2 = sb.append(true);StringBuffer sb3 = sb.append("Huang");StringBuffer sb4 = sb.append(928);System.out.println(sb.toString()); //StringBuffer類中重寫了toString方法,顯示的是對象中的屬性值System.out.println(sb2.toString());System.out.println(sb3.toString());System.out.println(sb4.toString());}}

輸出:

trueHuang928 trueHuang928 trueHuang928 trueHuang928
  • 例子2
package com.guiyang.restudy3;public class Demo2StringBuffer {public static void main(String[] args) {Demo1();Demo2();}private static void Demo2() {StringBuffer sb = new StringBuffer("1234");sb.insert(3, "Huang"); //在指定位置添加元素,如果沒有指定位置的索引就會報索引越界異常System.out.println(sb);}private static void Demo1() {StringBuffer sb = new StringBuffer();StringBuffer sb2 = sb.append(true);StringBuffer sb3 = sb.append("Huang");StringBuffer sb4 = sb.append(928);System.out.println(sb.toString()); //StringBuffer類中重寫了toString方法,顯示的是對象中的屬性值System.out.println(sb2.toString());System.out.println(sb3.toString());System.out.println(sb4.toString());}}

輸出:

trueHuang928 trueHuang928 trueHuang928 trueHuang928 123Huang4

總結

以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。