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


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


StringBuilder类的charAt(int index)方法用于返回StringBuilder Object包含的String的指定索引处的字符。索引值应介于0和length()-1之间。

用法:

public char charAt(int index)

参数:此方法接受一个int类型参数索引,该索引表示要返回的字符的索引。


返回值:此方法在指定位置返回字符。

异常:当index为负或大于或等于length()时,此方法将引发IndexOutOfBoundsException。

下面的程序演示StringBuilder类的charAt()方法:

示例1:

// Java program to demonstrate 
// the charAt() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        StringBuilder str = new StringBuilder(); 
  
        // add the String to StringBuilder Object 
        str.append("Geek"); 
  
        // get char at position 1 
        char ch = str.charAt(1); 
  
        // print the result 
        System.out.println("StringBuilder Object"
                           + " contains = " + str); 
        System.out.println("Character at Position 1"
                           + " in StringBuilder = " + ch); 
    } 
}
输出:
StringBuilder Object contains = Geek
Character at Position 1 in StringBuilder = e

示例2:

// Java program demonstrate 
// the charAt() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder 
            str 
            = new StringBuilder("WelcomeGeeks"); 
  
        // print string 
        System.out.println("String is "
                           + str.toString()); 
  
        // loop through string and print every Character 
        for (int i = 0; i < str.length(); i++) { 
  
            // get char at position i 
            char ch = str.charAt(i); 
  
            // print char 
            System.out.println("Char at position "
                               + i + " is " + ch); 
        } 
    } 
}
输出:
String is WelcomeGeeks
Char at position 0 is W
Char at position 1 is e
Char at position 2 is l
Char at position 3 is c
Char at position 4 is o
Char at position 5 is m
Char at position 6 is e
Char at position 7 is G
Char at position 8 is e
Char at position 9 is e
Char at position 10 is k
Char at position 11 is s

示例3:演示IndexOutOfBoundException

// Java program to demonstrate 
// IndexOutOfBound exception thrown by the charAt() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            // create a StringBuilder object 
            // with a String pass as parameter 
            StringBuilder 
                str 
                = new StringBuilder("WelcomeGeeks"); 
  
            // get char at position i 
            char ch = str.charAt(str.length()); 
  
            // print char 
            System.out.println("Char at position "
                               + str.length() + " is "
                               + ch); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException:
 String index out of range: 12

参考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#charAt(int)



相关用法


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