首页 > 编程知识 正文

java中charAt方法,string类charat方法作用

时间:2023-05-03 07:26:59 阅读:285948 作者:1791

charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法. charAt()方法返回指定索引位置的char值。索引范围为0~length()-1. 如: str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.

package com.xumingjie.s;


public class Cartat {
 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(6));
 }

}

结果如下:

T
 
s

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。