当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java StringBuilder substring()用法及代码示例


在StringBuilder类中,根据传递给它的参数,有两种类型的子字符串方法。

substring(int start)

StringBuilder类的substring(int start)方法是一种内置方法,用于从索引开头返回一个子字符串,并扩展到此序列的结尾。此方法返回的字符串包含从旧序列的索引开始到结尾的所有字符。

用法:


public String substring(int start)

参数:此方法仅接受一个参数开始,即整数类型值是指子字符串的开始索引。

返回值:此方法返回位于旧序列开始到结束范围内的子字符串。

异常:如果start小于零或大于此对象的长度,则此方法将引发StringIndexOutOfBoundsException。

以下示例程序旨在说明StringBuilder substring()方法:

示例1:

// Java program to demonstrate 
// the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("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 StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("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)

StringBuilder类的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。

以下示例程序旨在说明StringBuilder.substring()方法:

示例1:

// Java program to demonstrate 
// the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("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 StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("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/StringBuilder.html#substring(int, int)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder substring() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。