charat方法在Java中的返回值

   2024-09-30 5250
核心提示:在Java中,charAt() 方法是 String 类的一个成员方法public char charAt(int index)该方法接受一个整数参数 index,表示字符串中

在Java中,charAt() 方法是 String 类的一个成员方法

public char charAt(int index)

该方法接受一个整数参数 index,表示字符串中要访问的字符的位置。如果 index 在字符串的有效范围内(0 <= index< string.length()),则返回该位置上的字符。否则,将抛出 IndexOutOfBoundsException 异常。

以下是一个简单的示例:

public class Main {    public static void main(String[] args) {        String str = "Hello, world!";        char ch = str.charAt(4); // 获取索引为4的字符,即'o'        System.out.println("Character at index 4: " + ch);    }}

输出结果:

Character at index 4: o

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号