當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。