CodeGym /Java 博客 /随机的 /Java 字符串 charAt()
John Squirrels
第 41 级
San Francisco

Java 字符串 charAt()

已在 随机的 群组中发布

Java String charAt() 方法是什么?

Java 字符串类charAt()方法返回字符串指定索引处的字符。此指定索引作为参数提供,范围从 0 到 n-1,其中 n 表示字符串的长度。 句法
public char charAt(int index)
参数 charAt ()方法接收一个正整数,因为长度永远不会低于 0。 返回 它始终返回指定索引的 char 值,但如果参数值为负数或大于字符串长度,则会引发异常。

Java String charAt() 方法示例

class Main {
  public static void main(String[] args) {
    String str = "Java charAt() method example";

    // picking the A character of the string str at index 9 using charAt() method
    System.out.println(str.charAt(9));

    // picking the ( character of the string str at index 11 using charAt() method
    System.out.println(str.charAt(11));

    // picking the ) character of the string str at index 12 using charAt() method
    System.out.println(str.charAt(12));
  }
}
输出
БA ( )

结论

我们希望现在您了解什么是 Java 字符串charAt()方法以及如何实现它。请随意练习,并在需要更多帮助时返回。快乐学习!
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION