在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 = "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内部使用Systemarraycopy方法。 您可以从下面的方法实现中看到这一点。

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示例程序的全部操作。

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

Reference: toCharArray API Doc

参考: toCharArray API文档

翻译自: https://www.journaldev.com/766/string-to-char-array-to-string-java

  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值