StringBuffer类的subSequence(int start,int end)方法是一种内置方法,用于返回位于此序列的索引start和end-1之间的字符的子序列。子序列以索引开头的char值开始,以(end-1)的char值结束。返回的子序列的长度为end-start。因此,如果开始等于结束,则返回一个空的子序列。
用法:
public CharSequence subSequence?(int start, int end)
参数:
此方法接受两个参数:
- startInteger类型值是指子序列的起始endex。
- end整数类型值是指子序列的最后一个endex。
返回值:
此方法返回范围从start到end-1的指定子序列。
异常:
如果start或end为负,或者end大于length(),或者start大于end,则抛出IndexOutOfBoundsException。
以下程序说明了java.lang.StringBuffer.subSequence()方法:
示例1:
// Java program to demonstrate
// the subSequence() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("WelcomeGeeks");
// print string
System.out.println("String contains = " + str);
// get subSequence bewteen index 0 to 7
// using subSequence() and print
System.out.println("SubSequence = "
+ str.subSequence(0, 7));
}
}
输出:
String length = 12 and contains = WelcomeGeeks SubSequence = Welcome
示例2:
// Java program to demonstrate
// the subSequence() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("Indian Team Played Well");
// print string
System.out.println("String contains = " + str);
// get subSequence bewteen index 0 to 7
// using subSequence() and print
System.out.println("SubSequence = "
+ str.subSequence(7, 18));
}
}
输出:
String contains = Indian Team Played Well SubSequence = Team Played
示例3:当开始>结束时:
// Java program to demonstrate
// Exception thrown by the subSequence() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("Indian Team Played Well");
try {
// get subSequence bewteen index 0 to 7
// using subSequence() and print
System.out.println(str.subSequence(19, 18));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.AbstractStringBuffer.substring(AbstractStringBuffer.java:935) at java.lang.StringBuffer.substring(StringBuffer.java:76) at java.lang.AbstractStringBuffer.subSequence(AbstractStringBuffer.java:912) at java.lang.StringBuffer.subSequence(StringBuffer.java:76) at GFG.main(File.java:16)
参考文献:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#subSequence(int,int)
相关用法
- Java StringBuffer setLength()用法及代码示例
- Java StringBuffer append()用法及代码示例
- Java StringBuffer getChars()用法及代码示例
- Java StringBuffer toString()用法及代码示例
- Java StringBuffer setCharAt()用法及代码示例
- Java StringBuffer ensureCapacity()用法及代码示例
- Java StringBuffer offsetByCodePoints()用法及代码示例
- Java StringBuffer codePointAt()用法及代码示例
- Java StringBuffer reverse()用法及代码示例
- Java StringBuffer codePointBefore()用法及代码示例
- Java StringBuffer codePointCount()用法及代码示例
- Java StringBuffer lastIndexOf()用法及代码示例
- Java StringBuffer substring()用法及代码示例
- Java StringBuffer trimToSize()用法及代码示例
- Java StringBuffer replace()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuffer subSequence() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。