Java StringBuilder substring() 方法



Java StringBuilder substring() 方法用於從 StringBuilder 物件中檢索所需的子序列。子字串是給定字串的一小部分。預設情況下,此子字串從指定的索引開始,並擴充套件到此序列的末尾。

substring() 方法接受一個整數引數,該引數儲存起始索引的值。如果起始索引值為負或大於序列長度,則會引發異常。

substring() 方法有兩個多型變體,它們具有不同的引數,如下面的語法所示。

語法

以下是Java StringBuilder substring() 方法的語法:

public String substring(int start) 
public String substring(int start, int end)

引數

  • start − 這是起始索引(包含)。
  • end − 這是結束索引(不包含)。

返回值

此方法返回從起始索引到序列末尾的新字串。

如果將end值作為引數傳遞,則此方法返回從起始索引到結束索引的新字串。

示例:從 StringBuilder 字串中獲取子字串

如果給定的起始索引值為正數小於給定的序列長度,則 substring() 方法將返回一個新的子字串。

在下面的程式中,我們使用“Java Language”的值例項化StringBuilder 類。然後,使用substring() 方法,我們嘗試從指定起始索引 6的給定序列中檢索新的子字串

public class SubString {
   public static void main(String[] args) {
      //instantiate of the StringBuilder class
      StringBuilder sb = new StringBuilder("Java Language");
      System.out.println("The given string: " + sb);
      //initialize the start index value
      int startIndex = 5;
      System.out.println("The given start index value is: " + startIndex);
      //using the substring() method
      System.out.println("The new sub-string is: " + sb.substring(startIndex));
   }
}

輸出

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

The given string: Java Language
The given start index value is: 5
The new sub-string is: Language

示例:從 StringBuilder 字串中獲取子字串時遇到 StringIndexOutOfBoundsException

如果給定的起始索引值大於序列長度,則 substring() 方法將丟擲StringIndexOutOfBoundsException異常。

在下面的示例中,我們使用“Tutorix”的值建立一個StringBuilder 類的物件。使用substring() 方法,我們嘗試從指定起始索引 10的給定序列中檢索子字串,其值大於序列長度。

public class SubString {
   public static void main(String[] args) {
      try {
         //create an object of this class
         StringBuilder sb = new StringBuilder("Tutorix");
         System.out.println("The given string: " + sb);
         //initialize the start index value
         int startIndex = 10;
         System.out.println("The given start index value is: " + startIndex);
         //using the substring() method
         System.out.println("The new sub-string is: " + sb.substring(startIndex));
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given string: Tutorix
The given start index value is: 10
java.lang.StringIndexOutOfBoundsException: Range [10, 7) out of bounds for length 7
   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.substring(AbstractStringBuilder.java:1057)
   at java.base/java.lang.StringBuilder.substring(StringBuilder.java:525)
   at java.base/java.lang.StringBuilder.substring(StringBuilder.java:507)
   at SubString.main(SubString.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 7) out of bounds for length 7

示例:從 StringBuilder 字串中獲取子字串時遇到 StringIndexOutOfBoundsException

如果給定的起始索引值為負數,則此方法將丟擲StringIndexOutOfBoundsException異常。

在此程式中,我們使用“JavaProgramming”的值例項化StringBuilder 類。然後,使用substring() 方法,我們嘗試從指定起始索引 -1的給定序列中檢索子字串。

public class SubString {
   public static void main(String[] args) {
      try {
         //instantiate of the StringBuilder class
         StringBuilder sb = new StringBuilder("JavaProgramming");
         System.out.println("The given string: " + sb);
         //initialize the start index value
         int startIndex = -1;
         System.out.println("The given start index value is: " + startIndex);
         //using the substring() method
         System.out.println("The new sub-string is: " + sb.substring(startIndex));
      } catch(StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果:

The given string: JavaProgramming
The given start index value is: -1
java.lang.StringIndexOutOfBoundsException: Range [-1, 15) out of bounds for length 15
   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.substring(AbstractStringBuilder.java:1057)
   at java.base/java.lang.StringBuilder.substring(StringBuilder.java:525)
   at java.base/java.lang.StringBuilder.substring(StringBuilder.java:507)
   at SubString.main(SubString.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 15) out of bounds for length 15

示例:從 StringBuilder 字串中獲取子字串

如果給定的起始索引和結束索引值為正數小於序列長度,則 substring() 方法將返回新的子字串。

在此程式中,我們使用“Java Programming Language”的值建立一個StringBuilder 類的物件。然後,使用substring() 方法,我們嘗試從指定起始索引 5結束索引 16的給定序列中檢索子字串。

public class SubString {
   public static void main(String[] args) {
      //instantiate of the StringBuilder class
      StringBuilder sb = new StringBuilder("Java Programming Language");
      System.out.println("The given string: " + sb);
      //initialize the start index and end index values
      int startIndex = 5;
      int endIndex = 16;
      System.out.println("The given start index and end index values are: " + startIndex + " and " + endIndex);
      //using the substring() method
      System.out.println("The new sub-string is: " + sb.substring(startIndex, endIndex));
   }
}

輸出

執行上述程式後,將生成以下輸出:

The given string: Java Programming Language
The given start index and end index values are: 5 and 16
The new sub-string is: Programming
java_lang_stringbuilder.htm
廣告