- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包其他內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java StringBuffer subSequence() 方法
Java StringBuffer subSequence() 方法用於從 StringBuffer 物件中檢索子序列。子序列是字串或序列的一小部分。
subsequence() 方法接受兩個整數引數,分別表示開始和結束索引值。如果開始索引或結束索引值為負數,開始索引值大於結束索引值,以及結束索引值大於序列長度,則會丟擲異常。
語法
以下是 Java StringBuffer subSequence() 方法的語法:
public CharSequence subSequence(int start, int end)
引數
- start - 這是開始索引,包含在內。
- end - 這是結束索引,不包含在內。
返回值
此方法返回指定的子序列。
示例:獲取 StringBuffer 字串的子序列
如果開始索引和結束索引值為正數且小於序列長度,則 subsequence() 方法會返回給定序列的子序列。
在以下程式中,我們使用“Java Programming”的值例項化StringBuffer 類。然後,使用subsequence() 方法,我們嘗試在指定的開始索引 5和結束索引 16處檢索給定序列的子序列。
public class SubSequence {
public static void main(String[] args) {
//instantiate the StringBuffer class
StringBuffer sb = new StringBuffer("Java Programming");
System.out.println("The string is: " + sb.toString());
//initialize the startIndex and endIndex values
int startIndex = 5;
int endIndex = 16;
System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
//using the subSequence() method
System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
}
}
輸出
執行上述程式後,將產生以下結果:
The string is: Java Programming The initialize values of the startIndex and endIndex are: 5 and 16 The sub-sequence of the given sequence is: Programming
示例:在獲取 StringBuffer 字串的子序列時遇到 StringIndexOutOfBoundsException
如果給定的開始索引和結束索引值為負數,則此方法會丟擲StringIndexOutOfBoundsException異常。
在以下程式中,我們使用“Tutorials Point”的值建立一個StringBuffer 類的物件。使用subsequence() 方法,我們嘗試在指定的開始索引 -1和結束索引 -2處檢索給定序列的子序列。
public class SubSequence {
public static void main(String[] args) {
try {
//create an object of the StringBuffer class
StringBuffer sb = new StringBuffer("Tutorials Point");
System.out.println("The string is: " + sb.toString());
//initialize the startIndex and endIndex values
int startIndex = -1;
int endIndex = -2;
System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
//using the subSequence() method
System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
輸出
以下是上述程式的輸出:
The string is: Tutorials Point The initialize values of the startIndex and endIndex are: -1 and -2 java.lang.StringIndexOutOfBoundsException: Range [-1, -2) 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.StringBuffer.subSequence(StringBuffer.java:516) at SubSequence.main(SubSequence.java:15) Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, -2) out of bounds for length 15
示例:在獲取 StringBuffer 字串的子序列時遇到 StringIndexOutOfBoundsException
如果結束索引值大於序列長度,則 subsequence() 方法會丟擲StringIndexOutOfBoundsException異常。
在此程式中,我們使用以下值例項化StringBuffer 類
“Hello World”
使用subsequence() 方法,我們嘗試在指定的開始索引 5和結束索引 20處檢索給定序列的子序列。
public class SubSequence {
public static void main(String[] args) {
try {
//instantiate the StringBuffer class
StringBuffer sb = new StringBuffer("Hello World");
System.out.println("The string is: " + sb.toString());
//initialize the startIndex and endIndex values
int startIndex = 2;
int endIndex = 25;
System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
//using the subSequence() method
System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
輸出
上述程式產生以下結果:
The string is: Hello World The initialize values of the startIndex and endIndex are: 2 and 25 java.lang.StringIndexOutOfBoundsException: Range [2, 25) out of bounds for length 11 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.StringBuffer.subSequence(StringBuffer.java:516) at SubSequence.main(SubSequence.java:15) Exception: java.lang.StringIndexOutOfBoundsException: Range [2, 25) out of bounds for length 11
java_lang_stringbuffer.htm
廣告