第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能
學習筆記,僅供參考
文章目錄
- StringBuffer類
- StringBuffer的替換和反轉功能
- replace方法
- reverse方法
- StringBuffer的截取功能
- substring方法
- 舉幾個例子
StringBuffer類
StringBuffer的替換和反轉功能
replace方法
public StringBuffer replace(int start,int end,String str)
Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)
-
參數
- start - The beginning index, inclusive.
- end - The ending index, exclusive.
- str - String that will replace previous contents.
-
返回
- This object.
reverse方法
public StringBuffer reverse()
Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed. Let n be the character length of this character sequence (not the length in char values) just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.
- 返回
- a reference to this object.
StringBuffer的截取功能
substring方法
public String substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.
-
參數
- start - The beginning index, inclusive.
-
返回
- The new string.
public String substring(int start,int end)
Returns a new String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end - 1
-
參數
- start - The beginning index, inclusive.
- end - The ending index, exclusive.
-
返回
- The new string.
舉幾個例子
package com.guiyang.restudy3;public class D4StringBuffer {public static void main(String[] args) {Demo1();Demo2(); }private static void Demo2() {System.out.println("-------");StringBuffer sb = new StringBuffer("HuangBlack"); String str = sb.substring(5); //返回值類型不再是StringBuffer,而是StringSystem.out.println(str);System.out.println(sb);System.out.println("-------");String str3 = sb.substring(5, 7);System.out.println(str3);}private static void Demo1() {StringBuffer sb = new StringBuffer("HuangBlack"); sb.replace(0, 5, "Bai"); //替換System.out.println(sb);sb.reverse();System.out.println(sb);}}
輸出:
BaiBlack kcalBiaB ------- Black HuangBlack ------- Bl總結
以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三次学JAVA再学不好就吃翔(part
- 下一篇: 好听的微信名字大全,微信昵称大全470个