Java StringBuilder charAt()方法及示例

Java StringBuilder charAt()方法及示例

StringBuilder类的 charAt(int index) 方法用于返回StringBuilder对象所包含的字符串的指定索引处的字符。索引值应该在0和length()-1之间。

语法

public char charAt(int index)

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

返回值: 该方法返回 指定位置的字符

异常: 当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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程