如何在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
How to convert char[] to string in java?
提问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
回答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);