Skip to content
Fix Code Error

How to convert a char array back to a string?

March 13, 2021 by Code Error
Posted By: Anonymous

I have a char array:

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?

Solution

No, that solution is absolutely correct and very minimal.

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.

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.

Answered By: Anonymous

Related Articles

  • How can I resolve Web Component Testing error?
  • Fastest way to iterate over all the chars in a String
  • Unable to run Robolectric and Espresso with a…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Is it possible to apply CSS to half of a character?
  • When I'm testing a web app by JUnit and Mockito I…
  • Eclipse will not start and I haven't changed anything
  • ClassNotFoundException:…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Neither BindingResult nor plain target object for…
  • Jetpack Compose and Hilt Conflict
  • SecurityException: Permission denied (missing…
  • SQLException: No suitable Driver Found for…
  • java IO Exception: Stream Closed
  • How to set HTML5 required attribute in Javascript?
  • Examples of GoF Design Patterns in Java's core libraries
  • Gradle error: Execution failed for task…
  • SQL query return data from multiple tables
  • SQL JOIN and different types of JOINs
  • Requested bean is currently in creation: Is there an…
  • Can't access Eclipse marketplace
  • Using enums in a spring entity
  • How to make Javascript font responsive?
  • android studio 0.4.2: Gradle project sync failed error
  • Execution failed for task…
  • splitting.js results in "Uncaught TypeError:…
  • org.gradle.api.tasks.TaskExecutionException:…
  • no match for ‘operator
  • How to parse JSON file with Spring
  • How to convert java.util.Date to java.sql.Date?
  • Launching Spring application Address already in use
  • Android- Error:Execution failed for task…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • How to do code splitting using Svelte without Sapper
  • PHP: How to remove all non printable characters in a string?
  • Configure hibernate to connect to database via JNDI…
  • Could not install Gradle distribution from…
  • JPA Hibernate Persistence exception…
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • How do I install Java on Mac OSX allowing version switching?
  • Vaadin Spring Boot - There was an exception while…
  • ClassNotFoundException thrown
  • How does PHP 'foreach' actually work?
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • Error inflating class android.support.v7.widget.Toolbar?
  • Ukkonen's suffix tree algorithm in plain English
  • java.lang.ClassNotFoundException: HttpServletRequest
  • Eclipse fails to open .vue files in Web Page Editor
  • "Non-resolvable parent POM: Could not transfer…
  • What's the difference between Instant and LocalDateTime?
  • What's wrong with 'template int compare(char p1 [N],…
  • java.lang.RuntimeException: Unable to instantiate…
  • How to add Typescript to a Nativescript-Vue project?
  • How do I include certain conditions in SQL Count
  • INNER JOIN vs LEFT JOIN performance in SQL Server
  • iOS 8 removed "minimal-ui" viewport property, are…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • Java ElasticSearch None of the configured nodes are…
  • What is your most productive shortcut with Vim?
  • UnsatisfiedDependencyException: Error creating bean…
  • Injection of autowired dependencies failed;
  • Problems using Maven and SSL behind proxy
  • Plugin…
  • Spring Boot - Cannot determine embedded database…
  • What does "Could not find or load main class" mean?
  • Gradle: Execution failed for task ':processDebugManifest'
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • Length of string in bash
  • Address already in use: JVM_Bind
  • Spring schemaLocation fails when there is no…
  • Eclipse - Run in Debug Mode fails
  • What is a stack trace, and how can I use it to debug…
  • How to convert number to words in java
  • Removing a list of characters in string
  • Failed to execute goal…
  • javax.el.PropertyNotFoundException: Property 'foo'…
  • Usage of __slots__?
  • What is the origin of foo and bar?
  • Getting weird compilation error in defining a…
  • How to obtain the start time and end time of a day?
  • Multipart File Upload Using Spring Rest Template +…
  • data.table vs dplyr: can one do something well the…
  • RxJava Main Thread Always Crash
  • Spring Security exclude url patterns in security…
  • NullpointerException error while working with…
  • What's causing my java.net.SocketException:…
  • How do i update a javascript variable as its value changes?
  • Reference - What does this regex mean?
  • Exception : AAPT2 error: check logs for details
  • Name [jdbc/mydb] is not bound in this Context
  • Java.lang.NoClassDefFoundError:…
  • Jackson cannot parse body of a POST request
  • Can't find why this datetime test fails, in F#
  • How to install JSTL? The absolute uri:…
  • Jetty server throws idle timeout for REST calls
  • How to disable SSL certificate checking with Spring…
  • How to filter a RecyclerView with a SearchView
  • Parcelable encountered IOException writing…
  • javax.faces.application.ViewExpiredException: View…
  • Why does Spring security throw exception…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Evaluate empty or null JSTL c tags

Next Post:

Undoing a ‘git push’

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error