目录

char charAt(int index)

描述 (Description)

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

声明 (Declaration)

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

public char charAt(int index)

参数 (Parameters)

index - 这是char值的索引。

返回值 (Return Value)

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

异常 (Exception)

IndexOutOfBoundsException - 如果index参数为负数或不小于此字符串的长度。

例子 (Example)

以下示例显示了java.lang.String.charAt()方法的用法。

package com.iowiki;
import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "This is iowiki";
      // 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
↑回到顶部↑
WIKI教程 @2018