将字符串中的字符复制到 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