第三次学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
輸出:
trueHuang928 trueHuang928 trueHuang928 trueHuang928- 例子2
輸出:
trueHuang928 trueHuang928 trueHuang928 trueHuang928 123Huang4總結
以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生态土鸡宣传文案30句
- 下一篇: tcl王牌电视机如何连接电脑