如何在java中将char []转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19238065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:20:09  来源:igfitidea点击:

How to convert char[] to string in java?

javastring

提问by codepig

char[] c = string.toCharArray();

char[] c = string.toCharArray();

but how to convert c back to String type? thank you!

但是如何将 c 转换回 String 类型?谢谢你!

采纳答案by arshajii

You can use String.valueOf(char[]):

您可以使用String.valueOf(char[])

String.valueOf(c)

Under the hood, this calls the String(char[])constructor. I always prefer factory-esque methods to constructors, but you could have used new String(c)just as easily, as several other answers have suggested.

在幕后,这会调用String(char[])构造函数。与构造函数相比,我总是更喜欢工厂式的方法,但是您可以new String(c)像其他几个答案所建议的那样轻松地使用它。



char[] c = {'x', 'y', 'z'};
String s = String.valueOf(c);

System.out.println(s);
xyz

回答by cmd

You can do the following:

您可以执行以下操作:

char[] chars = ...
String string = String.valueOf(chars);

回答by Doorknob

You could use

你可以用

char[] c = new char[] {'a', 'b', 'c'};
String str = new String(c); // "abc"

Docs

文档

回答by ilovepjs

You can write:

你可以写:

char[] c = {'h', 'e','l', 'l', 'o'};
String s = new String(c);

回答by Farlan

You can use the String constructor:

您可以使用 String 构造函数:

String(char[] value);

String(char[] value);