將字串中的字元複製到 Java 中的 char 陣列
假設我們有以下字串。
String str = "Demo Text!";
若要複製上述字串的一部分,請使用 getChars() 方法。
// copy characters from string into chArray =str.getChars( 0, 5, chArray, 0 );
以下是在 Java 中將字元從字串複製到 char 陣列的示例。
示例
public class Demo {
public static void main(String[] args) {
String str = "Demo Text!";
char chArray[] = new char[ 5 ];
System.out.println("String: "+str);
// copy characters from string into chArray
str.getChars( 0, 5, chArray, 0 );
System.out.print("Resultant character array...
");
for (char ch : chArray)
System.out.print( ch );
}
}輸出
String: Demo Text! Resultant character array... Demo
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP