How would I create a String using a Character array in Java?

broment :

What would be the most efficient way to construct a new String using a Character[]? It's easy enough to do with the primitive char[], but unfortunately the String constructor does not include an option for Character[]. Converting to a char[] seems cumbersome. Is there any efficient way to construct the String with just the Character[]?

Thanks.

Jacob G. :

One solution is to use a Stream with Collectors#joining:

Character[] chars = { 'H', 'e', 'l', 'l', 'o' };

String s = Arrays.stream(chars)
                 .map(Object::toString)
                 .collect(Collectors.joining());

System.out.println(s);

Output:

Hello

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

How do I correct Error: String[] is not initialized

为什么Java Character.toUpperCase / toLowerCase没有像String.toUpperCase / toLowerCase这样的Locale参数

如何用Java实现String.charAt(int i)?

Java转换:char [] array-> String

Java:为什么String.compareIgnoreCase()同时使用Character.toUpperCase()和Character.toLowerCase()?

How to Create query string in jquery?

从List <String>转换为List <Character> Java

How to create an array of classes and instance objects with it in Swift?

Scala how can I uppercase first character and lowercase others

Swift: Create String given length same character

Scala Array [String]转换为Java String varargs

java.lang.IllegalArgumentException:无法获取array <string>的JDBC类型

Swift错误无法将类型“ [String.Element]”(也称为“ Array <Character>”)的值转换为预期的参数类型“ [String]”

How do I select a random array, and then a random index inside THAT array?

Java String [] array = line.split(“”)其中line只是空白

How do I create PowerBI charts by typing a description of the chart

How to create idendical variables in MATLAB from an array of variable names?

EncodedImage create(String i)解释

Java array [i] ++与++ array [i]

Java中的string [i]和string.charAt(i)有什么区别?

Java中String.value0f和Character.toString之间的不同行为

不兼容的类型。找到:“java.lang.Enum”,需要:“char、byte、short、int、Character、Byte、Short、Integer、String 或枚举”

Problem with forming string array using loop

How do I avoid an 'unbound variable' error when the query is unknown at compile but known when the function would be run?

How can I create a PR across forks using the Azure DevOps Service API?

How do I initialize a pointer to an Array of structure

I want to extract a string after the specific character is the character exists in sql

Why am I getting string reversal wrong with a trailing space character?

How do I get MongoDB to pull items from an array using bulkWrite?