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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能

發(fā)布時間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

學(xué)習(xí)筆記,僅供參考


文章目錄

    • StringBuffer類
      • StringBuffer的替換和反轉(zhuǎn)功能
        • replace方法
        • reverse方法
      • StringBuffer的截取功能
        • substring方法
      • 舉幾個例子



StringBuffer類


StringBuffer的替換和反轉(zhuǎn)功能


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.)

  • 參數(shù)

    • 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.

  • 參數(shù)

    • 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

  • 參數(shù)

    • 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

總結(jié)

以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part56)--StringBuffer的替换反转和截取功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。