Java StringBuffer replace() 方法



Java StringBuffer replace() 方法用於將 StringBuffer 物件的子字串中的字元替換為指定字串中的字元。子字串從指定的開始位置開始,擴充套件到索引 end - 1處的字元,如果不存在這樣的字元,則擴充套件到序列的末尾。

replace() 方法接受三個引數作為整數和字串,分別儲存start、end 和 str的值。如果開始索引值為負或大於字串長度,則會丟擲異常。

語法

以下是Java StringBuffer replace() 方法的語法:

public StringBuffer replace(int start, int end, String str)

引數

  • start - 這是開始索引,包含在內。

  • end - 這是結束索引,不包含在內。

  • str - 這是將替換先前內容的字串。

返回值

此方法返回此物件。

示例:用另一個字串替換子字串

如果給定的開始索引值為正,並且小於字串長度,則 replace() 方法將替換指定字串中的字元。

在下面的程式中,我們使用值“Java Programming”例項化StringBuffer 類。然後,使用replace() 方法,我們嘗試替換此序列中子字串“Language”的字元,該子字串的startIndex 為 5endIndex 為 16

public class Replace {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("Java Programming");
      System.out.println("The given String is: " + sb);
      //initialize the startIndex, endIndex, and string value
      int startIndex = 5;
      int endIndex = 16;
      String str = "Language";
      System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
      System.out.println("The given sub-string is: " + str);
      //using the replace() method
      System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
   }
}

輸出

執行上述程式後,將產生以下結果:

The given String is: Java Programming
The given startIndex and endIndex values are: 5 and 16
The given sub-string is: Language
After replace the string: Java Language

示例:在用另一個字串替換子字串時遇到 IndexOutOfBoundException

如果給定的startIndex 值為負,則 replace() 方法將丟擲IndexOutOfBoundException

在下面的示例中,我們使用值“TutorialsPoint”建立一個StringBuffer 類的物件。使用replace() 方法,我們嘗試替換此序列中子字串“Point”的字元,該子字串的startIndex 為 -1endIndex 為 10

public class Replace {
   public static void main(String[] args) {
      try {
         //create an object StringBuffer class
         StringBuffer sb = new StringBuffer("TutorialsPoint");
         System.out.println("The given String is: " + sb);
         //initialize the startIndex, endIndex, and string value
         int startIndex = -1;
         int endIndex = 10;
         String str = "India";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given String is: TutorialsPoint
The given startIndex and endIndex values are: -1 and 10
The given sub-string is: India
java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987)
   at java.base/java.lang.StringBuffer.replace(StringBuffer.java:497)
   at Replace.main(Replace.java:16)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14

示例:在用另一個字串替換子字串時遇到 IndexOutOfBoundException

如果給定的開始索引值大於字串長度,則此方法將丟擲IndexOutOfBoundException

在此示例中,我們使用值“hello”例項化StringBuffer 類。使用replace() 方法,我們嘗試替換此序列中子字串“llo”的字元,該子字串的startIndex 為 10endIndex 為 5

public class Replace {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("hello");
         System.out.println("The given String is: " + sb);
         //initialize the startIndex, endIndex, and string value
         int startIndex = 10;
         int endIndex = 5;
         String str = "world";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下輸出:

The given String is: hello
The given startIndex and endIndex values are: 10 and 5
The given sub-string is: world
java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987)
   at java.base/java.lang.StringBuffer.replace(StringBuffer.java:497)
   at Replace.main(Replace.java:16)
Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5
java_lang_stringbuffer.htm
廣告