Today we will learn how to convert String to a char array and then char array to String in Java.
今天,我们将学习如何在Java中将String转换为char数组,然后将char数组转换为String。
字符串到char数组 (String to char array)
Java String is a stream of characters. String class provides a utility method to convert String to a char array in java. Let’s look at this with a simple program.
Java String是字符流。 String类提供了一种实用程序方法,可以将String转换为Java中的char数组。 让我们用一个简单的程序来看一下。
package com.journaldev.util;
import java.util.Arrays;
public class StringToCharArray {
public static void main(String[] args) {
String str = "journaldev.com";
char[] charArr = str.toCharArray();
// print the char[] elements
System.out.println("String converted to char array: "
+ Arrays.toString(charArr));
}
}
Below image shows the output produced by the above program.
下图显示了以上程序产生的输出。
String.toCharArray
internally use System class arraycopy
method. You can see that from below method implementation.
String.toCharArray
内部使用System类arraycopy
方法。 您可以从下面的方法实现中看到这一点。
public char[] toCharArray() {
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
Notice the use of Arrays.toString
method to print the char array. Arrays
is a utility class in java that provides many useful methods to work with array. For example, we can use Arrays class to search, sort and java copy array operations.
注意使用Arrays.toString
方法来打印char数组。 Arrays
是Java中的实用程序类,它提供了许多有用的方法来处理数组。 例如,我们可以使用Arrays类来搜索,排序和Java复制数组操作。
char数组转换为String (char array to String)
Let’s look at a simple program to convert char array to String in Java.
让我们看一个简单的程序,用Java将char数组转换为String。
package com.journaldev.util;
public class CharArrayToString {
public static void main(String[] args) {
char[] charArray = {'P','A','N','K','A','J'};
String str = new String(charArray);
System.out.println(str);
}
}
Below image shows the output produced by char array to String program.
下图显示了char数组对String程序产生的输出。
We are using String class constructor that takes char array as an argument to create a String from a char array. However if you look at this constructor implementation, it’s using Arrays.copyOf
method internally.
我们正在使用String类构造函数,该构造函数将char数组作为参数从char数组创建String。 但是,如果您看一下此构造函数的实现,则它在内部使用Arrays.copyOf
方法。
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
Again Arrays.copyOf
method internally use System.arraycopy
native method.
同样, Arrays.copyOf
方法在内部使用System.arraycopy
本机方法。
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
So we can clearly see that System arraycopy() is the method being used in both Strings to char array and char array to String operations. That’s all for converting String to char array and char array to String example program.
因此,我们可以清楚地看到, System arraycopy()是在Strings到char数组和char array到String操作中使用的方法。 这就是将String转换为char数组并将char数组转换为String示例程序的全部操作。
Reference: toCharArray API Doc
翻译自: https://www.journaldev.com/766/string-to-char-array-to-string-java