Java - String getChars() 方法



描述

Java String getChars() 方法用於將此字串中的字元複製到目標字元陣列中。在 Java 中,陣列是一個儲存相似型別元素的物件。

getChars() 方法接受四個不同的引數,這些引數儲存源起始索引、源結束索引、目標字元陣列和目標起始索引的值。如果 srcBegin 值為負數,srcBegin 大於 srcEnd,srcEnd 大於字串等,則會丟擲異常。

第一個要複製的字元位於 srcBegin 索引處,最後一個要複製的字元位於 srcEnd-1 索引處。

語法

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

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

引數

  • srcBegin - 這是要複製的字串中第一個字元的索引。

  • srcEnd - 這是要複製的字串中最後一個字元之後的索引。

  • dst - 這是目標陣列。

  • dstBegin - 這是目標陣列中的起始偏移量。

返回值

此方法不返回值。

從字串獲取字元陣列示例

如果 srcBegin 值為正數,並且 srcEnd 值小於字串長度,則 getChars() 方法會將字串字元複製到字元陣列中。

在下面的程式中,我們使用“Welcome to TutorialsPoint”的值例項化一個字串類。使用getChars() 方法,我們嘗試將字串字元複製到字元陣列中,指定的srcIndex 為 5,srcEnd 為 15,dstbegin 為 0

package com.tutorialspoint;

import java.util.Arrays;

public class GetChars {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Welcome to TutorialsPoint");
      System.out.println("The given string is: " + str);
      
      // create character array
      char[] chararr = new char[10];
      
      // copies characters from starting and ending index into the destination character array
      
      //initialize the srcBegin, srcEnd, and dstBegin values
      int srcBegin = 5, srcEnd = 15, dstBegin = 0;
      System.out.println("The srcBegin, srcEnd, and dstBegin values are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin);
      
      //using the getChars() method
      str.getChars(srcBegin, srcEnd, chararr, dstBegin);
      
      // print the character array
      System.out.print("The Value of character array : " + Arrays.toString(chararr));
   }
}

輸出

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

The given string is: Welcome to TutorialsPoint
The srcBegin, srcEnd, and dstBegin values are: 5, 15, and 0
The Value of character array : [m, e,  , t, o,  , T, u, t, o]

從字串獲取字元陣列,起始索引為 -1 的示例

如果給定的 srcBegin 索引值為負數,則 getChars() 方法會丟擲StringIndexOutOfBoundsException異常。

在以下示例中,我們使用值“Hello World”建立了一個字串類的物件。然後,我們建立一個大小為 20的字元陣列。使用getChars() 方法,我們嘗試將字串字元從當前字串複製到字元陣列中,指定的srBegin 索引為 -1,srcEnd 索引為 5,dstBegin 索引為 0

package com.tutorialspoint;

import java.util.Arrays;

public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("Hello World");
         System.out.println("The given string is: " + str);
         
         // create character array
         char[] chararr = new char[20];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = -1, srcEnd = 5, dstBegin = 0;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given string is: Hello World
The srcBegin, secEnd, and dstBegin index value are: -1, 5, and 0
java.lang.StringIndexOutOfBoundsException: begin -1, end 5, length 11
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: begin -1, end 5, length 11

檢查從字串獲取字元陣列時發生的異常示例

如果 srcBegin 值大於 srcEnd,則此方法會丟擲StringIndexOutOfBoundsException異常。

在此示例中,我們使用值“Java Programming”建立一個字串。使用getChars() 方法,我們嘗試將字串字元從當前字串複製到字元陣列中,指定的srBegin 索引為 5,srcEnd 索引為 3,dstBegin 索引為 1。由於 serBegin 值大於 srEnd,因此該方法將丟擲異常。

import java.util.Arrays;

public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("Java Programming");
         System.out.println("The given string is: " + str);
         
         // create character array
         char[] chararr = new char[25];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = 5, srcEnd = 3, dstBegin = 1;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

輸出

上述程式產生以下輸出:

The given string is: Java Programming
The srcBegin, secEnd, and dstBegin index value are: 5, 3, and 1
java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: begin 5, end 3, length 16

檢查從字串獲取字元陣列時發生的異常示例

如果 srcEnd 值大於字串長度,則 getChars() 方法會丟擲StringIndexOutOfBoundsException異常。

在下面的程式中,我們使用值“TutorialsPoint”例項化字串類。使用getChars() 方法,我們嘗試將字串字元從此字串複製到字元陣列中,指定的 srBegin 索引為 1,srcEnd 索引為 15,其值大於字串長度。

import java.util.Arrays;

public class GetChars {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("TutorialsPoint");
         System.out.println("The given string is: " + str);
         System.out.println("The string length is: " + str.length());
         
         // create character array
         char[] chararr = new char[20];
         
         // copies characters from starting and ending index into the destination character array
         
         //initialize the srcBegin, srcEnd, and dstBegin index values
         int srcBegin = 1, srcEnd = 15, dstBegin = 0;
         System.out.println("The srcBegin, secEnd, and dstBegin index value are: " + srcBegin + ", " + srcEnd + ", and " + dstBegin );
         
         //using the getChars() method
         str.getChars(srcBegin, srcEnd, chararr, dstBegin);
         
         // print the character array
         System.out.print("The Value of character array : " + Arrays.toString(chararr));
      } catch (StringIndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.print("Exception: "  + e);
      }
   }
}

輸出

執行上述程式後,將生成以下輸出:

The given string is: TutorialsPoint
The string length is: 14
The srcBegin, secEnd, and dstBegin index value are: 1, 15, and 0
java.lang.StringIndexOutOfBoundsException: begin 1, end 15, length 14
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.getChars(String.java:1676)
	at com.tutorialspoint.String.GetChars.main(GetChars.java:18)
Exception: java.lang.StringIndexOutOfBoundsException: begin 1, end 15, length 14
java_lang_string.htm
廣告