如何将字符数组转换回字符串?

编程入门 行业动态 更新时间:2024-10-24 11:23:52
本文介绍了如何将字符数组转换回字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个字符数组:

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};

我目前的解决办法是做

My current solution is to do

String b = new String(a);

但肯定有这样做的更好的办法?

But surely there is a better way of doing this?

推荐答案

没有,该解决方案是完全正确和非常小的。

No, that solution is absolutely correct and very minimal.

不过请注意,这是一个非常不寻常的情况:因为字符串是专门在Java中处理,甚至foo的实际上是一个字符串。所以不需要在正常code代表将字符串分割成单独的字符和他们一起回来的必要。

Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.

比较这对C / C ++,其中foo的你的字符 S按零终止的包它们之间的字节一边和字符串在另一边,许多因转换遗留做的方法。

Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.

更多推荐

如何将字符数组转换回字符串?

本文发布于:2023-10-23 00:07:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   如何将   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!