String.charAt()

本文介绍了 Java 中 String 类的 charAt() 方法的使用方法及其参数、返回值和可能抛出的异常。通过示例代码展示了如何获取字符串中指定位置的字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述
java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推,


声明
以下是声明java.lang.String.charAt()方法


public char charAt(int index)
参数
index -- 这是该指数的char值.


返回值
此方法返回这个字符串的指定索引处的char值。第一个char值的索引为0.


异常
IndexOutOfBoundsException -- 如果index参数为负或不小于该字符串的长度.


实例
下面的示例演示使用的java.lang.String.charAt()方法.


package com.yiibai;


import java.lang.*;


public class StringDemo {


  public static void main(String[] args) {
  
    String str = "This is yiibai";
                   
    // prints character at 1st location
    System.out.println(str.charAt(0));
          
    // prints character at 5th location i.e white-space character
    System.out.println(str.charAt(4));
          
    // prints character at 18th location 
    System.out.println(str.charAt(17));
  }

让我们编译和运行上面的程序,这将产生以下结果。它也会打印空白字符。


T


p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值