在StringBuffer类中,取决于传递给它的参数,有两种类型的子字符串方法。
substring(int start)
StringBuffer类的substring(int start)方法是一种内置方法,用于从索引开头返回一个子字符串,并扩展到此序列的结尾。此方法返回的字符串包含从旧序列的索引开始到结尾的所有字符。
用法:
public String substring(int start)
参数:此方法仅接受一个参数开始,即整数类型值是指子字符串的开始索引。
返回值:此方法返回位于旧序列开始到结束范围内的子字符串。
异常:如果start小于零或大于此对象的长度,则此方法将引发StringIndexOutOfBoundsException。
以下示例程序旨在说明StringBuffer substring()方法:
示例1:
// Java program to demonstrate
// the substring() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("GeeksForGeeks");
// print string
System.out.println("String contains = "
+ str);
// get substring start from index 5
// using substring() and print results
System.out.println("SubSequence = "
+ str.substring(5));
}
}
String contains = GeeksForGeeks SubSequence = ForGeeks
示例2:演示StringIndexOutOfBoundsException
// Java program to demonstrate
// Exception thrown by the substring() 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 substring starts from index -7
// using substring() and print
str.substring(-7);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -7
substring(int start, int end)
StringBuffer类的substring(int start,int end)方法是一种内置方法,用于从索引start返回子字符串start并扩展到此序列的index end-1。此方法返回的字符串包含从旧序列的索引开始到索引结束1的所有字符。
用法:
public String substring(int start)
参数:此方法接受两个参数start,即Integer类型值引用子字符串的开始索引和end,后者也是Integer类型值引用子字符串的结束索引。
返回值:此方法返回位于旧序列索引开始到索引结束1的范围内的子字符串。
异常:如果start或end为负或大于length(),或者start大于end,则此方法引发StringIndexOutOfBoundsException。
以下示例程序旨在说明StringBuffer.substring()方法:
示例1:
// Java program to demonstrate
// the substring() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("GeeksForGeeks");
// print string
System.out.println("String contains = " + str);
// get substring start from index 5 to index 8
// using substring() and print results
System.out.println("SubSequence = "
+ str.substring(5, 8));
}
}
String contains = GeeksForGeeks SubSequence = For
示例2:演示StringIndexOutOfBoundsException
// Java program to demonstrate
// Exception thrown by the substring() 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 substring starts from index 7
// and end at index 3
// using substring() and print
str.substring(9, 3);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -6
参考文献:
- https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#substring(int, int)
- https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#substring(int)
相关用法
- Java StringBuffer getChars()用法及代码示例
- Java StringBuffer deleteCharAt()用法及代码示例
- Java StringBuffer codePointBefore()用法及代码示例
- Java StringBuffer setCharAt()用法及代码示例
- Java StringBuffer codePointCount()用法及代码示例
- Java StringBuffer trimToSize()用法及代码示例
- Java StringBuffer toString()用法及代码示例
- Java StringBuffer codePointAt()用法及代码示例
- Java StringBuffer ensureCapacity()用法及代码示例
- Java StringBuffer offsetByCodePoints()用法及代码示例
- Java StringBuffer appendCodePoint()用法及代码示例
- Java StringBuffer lastIndexOf()用法及代码示例
- Java StringBuffer append()用法及代码示例
- Java StringBuffer reverse()用法及代码示例
- Java StringBuffer replace()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuffer substring() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。