Java - String charAt() 方法



描述

Java String charAt() 方法用於檢索指定索引處的字元。索引指的是字元在序列中的位置。第一個字元值表示第 0 個索引,下一個字元值表示第 1 個索引,以此類推。索引從 0 開始,到 length()-1 結束。

字串是一個儲存字元序列的物件。java.lang.string 類表示 Java 中的字串。Java 中的字串是常量,建立後其值無法更改。

charAt() 方法接受一個整數引數,該引數儲存索引的值。如果索引值為負數或大於序列長度,則丟擲異常。

語法

以下是Java String charAt() 方法的語法:

public char charAt(int index)

引數

  • index - 這是字元值索引。

返回值

此方法返回此字串中指定索引處的字元值。第一個字元值位於索引 0 處。

從字串中獲取給定索引處的字元示例

如果索引值非負且小於字串長度,則charAt() 方法返回指定索引處的字元值。

在以下程式中,我們建立一個值為“This is tutorialspoint”的字串字面量。然後使用charAt() 方法,我們正在檢索指定索引0、4 和 17處的字元。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      
      //create an string literal
      String str = "This is tutorialspoint";
      System.out.print("The string is: " + str);
      
      //initialize the index values
      int index1 = 0;
      int index2 = 4;
      int index3 = 17;
      System.out.println("The index values are: " + index1 + ", " + index2 + " and " + index3);
      //using the charAt() method
      System.out.println("The character value at the " + index1 + " index is: " + str.charAt(index1));
      System.out.println("The character value at the " + index2 + " index is: " + str.charAt(index2));
      System.out.println("The character value at the " + index3 + " index is: " + str.charAt(index3));
   }
}

輸出

執行上述程式後,將產生以下結果:

The string is: This is tutorialspoint
The index values are: 0, 4 and 17
The character value at the 0 index is: T
The character value at the 4 index is:  
The character value at the 17 index is: p

從字串中獲取負數索引處的字元時出現異常示例

如果索引值為負數,則此方法會丟擲IndexOutOfBoundsException異常。

在此程式中,我們正在建立一個值為“Tutorials Point”的字串字面量。然後,使用charAt() 方法,我們嘗試檢索指定索引 -1處的字元值。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str = "Tutorials Point";
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = -1;
         System.out.println("The index value is: " + index);
         
         // use charAt() method
         System.out.println("The character value at the specifed index " + index + " is: " + str.charAt(index));
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The string value is: Tutorials Point
The index value is: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
	at java.base/java.lang.String.charAt(String.java:1515)
	at com.tutorialspoint.Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

從字串中獲取無效索引處的字元示例

如果索引值大於字串長度,則CharAt() 方法會丟擲IndexOutOfBoundsException異常。

在以下示例中,我們使用值為"Welcome to Tutorials Point"建立字串類的一個物件。然後,使用charAt() 方法,我們嘗試檢索指定索引處的字元,其值大於字串長度。

package com.tutorialspoint;

public class Test {
   public static void main(String[] args) {
      try {
         
         // create an object of the string
         String str = new String("Welcome to Tutorials Point");
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = 50;
         System.out.println("The index value is: " + index);
         
         // use charAt() method
         System.out.println("The character value at the specifed index " + index + " is: " + str.charAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果:

The string value is: Welcome to Tutorials Point
The index value is: 50
java.lang.StringIndexOutOfBoundsException: String index out of range: 50
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
	at java.base/java.lang.String.charAt(String.java:1515)
	at com.tutorialspoint.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 50

使用 For 迴圈檢索字串的所有字元示例

使用for 迴圈以及charAt() 方法,我們可以檢索指定索引處的全部字元。

在此程式中,我們使用值為"TutorialsPoint"例項化字串類。然後,迴圈將遍歷給定的序列。使用charAt() 方法,我們嘗試檢索指定索引處的全部字元。

package com.tutorialspoint;

public class ClassDemo {
   public static void main(String[] args) {
      
      // create an object of the String
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      System.out.print("The characters of the given string are: ");
      
      // using for loop along with the charAt() method
      for (int i = 0; i < str.length(); i++) {
         System.out.print(str.charAt(i) + " ");
      }
   }
}

輸出

The given string is: TutorialsPoint
The characters of the given string are: T u t o r i a l s P o i n t
java_lang_string.htm
廣告