1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 在Java中将字符串转换为char数组 将char数组转换为String

在Java中将字符串转换为char数组 将char数组转换为String

时间:2024-02-06 19:13:05

相关推荐

在Java中将字符串转换为char数组 将char数组转换为String

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 = "";char[] charArr = str.toCharArray();// print the char[] elementsSystem.out.println("String converted to char array: " + Arrays.toString(charArr));}}

Below image shows the output produced by the above program.

下图显示了以上程序产生的输出。

String.toCharArrayinternally use System classarraycopymethod. 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 ofArrays.toStringmethod to print the char array.Arraysis 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 usingArrays.copyOfmethod internally.

我们正在使用String类构造函数,该构造函数将char数组作为参数从char数组创建String。 但是,如果您看一下此构造函数的实现,则它在内部使用Arrays.copyOf方法。

public String(char value[]) {this.value = Arrays.copyOf(value, value.length);}

AgainArrays.copyOfmethod internally useSystem.arraycopynative 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示例程序的全部操作。

GitHub Repository.GitHub存储库中签出更多数组示例。

Reference: toCharArray API Doc

参考: toCharArray API文档

翻译自: /766/string-to-char-array-to-string-java

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。