- Various ways to convert Character[] array to String:
- 3. Using copyValueOf(chArray); method of String class
- Method signature:
- ConvertCharacterArrayIntoStringUsingCopyValueOfMethod.java
- Method signature:
- ConvertCharacterArrayIntoStringUsingObjectCreation.java
- 5. Using append() method of StringBuffer & StringBuilder class
- Syntax:
- ConvertCharacterArrayIntoStringByAppending.java
- Related Articles:
- References:
- Parse char array to string java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- 5 Ways to convert Java char array to string
- 1. Passing the char array to the String class constructor
- 2. Using the valueOf() method of String class
- 3. Using the copyValueOf() method of String class
- 4. Using append() method of StringBuilder class
- 5. Manually way
- Other interesting articles which you may like …
- Leave a Reply Cancel reply
- How to convert a char array to a string in Java
- You might also like.
Various ways to convert Character[] array to String:
3. Using copyValueOf(chArray); method of String class
Method signature:
public static String copyValueOf(char data[]);
ConvertCharacterArrayIntoStringUsingCopyValueOfMethod.java
package in.bench.resources.character.array.to.string.conversion; public class ConvertCharacterArrayIntoStringUsingCopyValueOfMethod < public static void main(String[] args) < // primitive char[] array char[] chArray1 = ; // 1. converting char[] array to String // by using copyValueOf() method String str1 = String.copyValueOf(chArray1); System.out.println("1. Converted String value is : " + str1); // primitive char[] array char[] chArray2 = new char[]; // 2. converting char[] array to String // by using copyValueOf() method String str2 = String.copyValueOf(chArray2); System.out.println("\n2. Converted String value is : " + str2); > >
1. Converted String value is : Bench 2. Converted String value is : Resources
3. Create String object and then pass char[] array in the constructor
- Here, we will create new String object and then pass char[] array object as constructor-argument
- After creating new String object by passing char[] array, then invoke toString() method for converting char[] array to String object
Method signature:
public String(char value[]); public String toString();
ConvertCharacterArrayIntoStringUsingObjectCreation.java
package in.bench.resources.character.array.to.string.conversion; public class ConvertCharacterArrayIntoStringUsingObjectCreation < public static void main(String[] args) < // primitive char[] array char[] chArray1 = ; // 1. converting char[] array to String // by creating String object String strObj1 = new String(chArray1); // and then invoke toString(); method String str1 = strObj1.toString(); System.out.println("1. Converted String value is : " + str1); // primitive char[] array char[] chArray2 = new char[]; // 2. converting char[] array to String // by creating String object String strObj2 = new String(chArray2); // and then invoke toString(); method String str2 = strObj2.toString(); System.out.println("\n2. Converted String value is : " + str2); > >
1. Converted String value is : Bench 2. Converted String value is : Resources
5. Using append() method of StringBuffer & StringBuilder class
- This method can be used to convert primitive char[] array to String object by appending to StringBuffer or StringBuilder object
- And then invoking toString() method
Syntax:
StringBuffer sb = sb.append(chArray); String temp = sb.toString();
ConvertCharacterArrayIntoStringByAppending.java
package in.bench.resources.character.array.to.string.conversion; public class ConvertCharacterArrayIntoStringByAppending < public static void main(String[] args) < // primitive char[] array char[] chArray1 = ; // 1. converting char[] array to String // by using append() method StringBuffer sb1 = new StringBuffer(); String str1 = sb1.append(chArray1).toString(); System.out.println("1. Converted String value is : " + str1); // primitive char[] array char[] chArray2 = new char[]; // 2. converting char[] array to String // by using append() method StringBuffer sb2 = new StringBuffer(); String str2 = sb2.append(chArray2).toString(); System.out.println("\n2. Converted String value is : " + str2); > >
1. Converted String value is : Bench 2. Converted String value is : Resources
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- String to int conversion – 3 ways
- Integer to String conversion – 6 ways
- String to float conversion – 3 ways
- Float to String conversion – 6 ways
- String to double conversion – 3 ways
- Double to String conversion – 6 ways
- String to long conversion – 3 ways
- Long to String conversion – 6 ways
- String to boolean conversion – 3 ways
- Boolean to String conversion – 6 ways
- String to char conversion
- Character to String conversion – 6 ways
- String to char[] array conversion – 4 ways
- Character[] array to String conversion – 5 ways
- String to byte conversion – 3 ways
- Byte to String conversion – 5 ways
- String to byte[] array conversion
- Byte[] array to String conversion
- String to short conversion – 3 ways
- Short to String conversion – 5 ways
- StringBuffer to String conversion and vice-versa
- StringBuilder to String conversion and vice-versa
- String to Date conversion
- Date to String conversion
References:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
- https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- https://docs.oracle.com/javase/tutorial/java/data/converting.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/Character.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
Happy Coding !!
Happy Learning !!
Parse char array to string java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
5 Ways to convert Java char array to string
In this article, we will look into the possible ways to convert Java char array to string.
- Passing the char array to the String class constructor
- Using the valueOf() method of String class
- Using the copyValueOf() method of String class
- Using append() method of StringBuilder class
- Manual way
1. Passing the char array to the String class constructor
In the below code we did the following
- We have a charArray which holds the characters ‘J’, ‘A’, ‘V’, ‘A’
- Pass the charArray to the String ‘str’ class constructor
- Finally print the string str
package com.javainterviewpoint; public class CharToString < public static void main(String[] args) < //Character array char[] charArray = new char[]; //Create a new String object and pass the char array to the constructor String str = new String(charArray); //Printing the string after conversion System.out.println(str); > >
2. Using the valueOf() method of String class
Just pass the charArray to the valueOf() method of the String class.
3. Using the copyValueOf() method of String class
This is similar to valueOf() method, instead of passing the charArray to valueOf() method we will be passing it to charValueOf() method
package com.javainterviewpoint; public class CharToString < public static void main(String[] args) < //Character array char[] charArray = new char[]; //Pass the charArray to copyValueOf() method String str = String.copyValueOf(charArray); //Printing the string after conversion System.out.println(str); > >
4. Using append() method of StringBuilder class
This is a less efficient way as it takes more memory
package com.javainterviewpoint; public class CharToString < public static void main(String[] args) < //Character array char[] charArray = new char[]; //Pass charArray to append() method StringBuilder sb = new StringBuilder().append(charArray); //Printing the string after conversion System.out.println(sb.toString()); > >
5. Manually way
Manual way this is not a recommended one.
package com.javainterviewpoint; public class CharToString < public static void main(String[] args) < //Character array char[] charArray = new char[]; String str=""; //Read the charArray and append it everytime for(char c : charArray) < str = str+c; >System.out.println(str); > >
Other interesting articles which you may like …
- Top 8 Java Interview Questions On main() Method
- Java Program to Print Floyd’s Triangle
- Top 26 Number Pattern Programs In Java
- JVM Architecture – Understanding JVM Internals
- Object and Object Class in Java
- Difference between JDK, JRE and JVM
- Components of Java Development Kit (JDK)
- What is a Class in Java with Example
- How to open .class file in Java
- How to Set Classpath for Java in Windows
- Difference Between ClassNotFoundException Vs NoClassDefFoundError In Java
- How HashMap works in Java
- How to make a class Immutable in Java
- Polymorphism in Java – Method Overloading and Overriding
- Types of polymorphism in java
- Types of Inheritance in Java
- Why Java does not supports Multiple Inheritance – Diamond Problem?
- Java Method Overloading Example
- Java Constructor Overloading Example
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
How to convert a char array to a string in Java
There are multiple ways to convert a character array ( char[] ) to a string in Java. You can use the string constructor, the String.valueOf() method and Streams API to convert a sequence of characters to a string object.
The easiest way to convert a character array to a string is using the string constructor e.g. new String(char[]) . The String class in Java provides an overloaded constructor that accepts a char array as an argument. Here is an example:
// Declare char array char[] chars = 'M', 'a', 'n', 'g', 'o'>; // Convert the char array to a string String str = new String(chars); System.out.println(str); // Mango
The String.valueOf() method is yet another simple way to convert a char[] to a string in Java. It returns the string representation of the passed argument, as shown below:
// Declare char array char[] chars = 'M', 'a', 'n', 'g', 'o'>; // Convert the char array to a string String str = String.valueOf(chars); System.out.println(str); // Mango
Java 8 provides the Arrays.stream() method to create a stream using an array of characters. Then, you can use the Collectors.joining() method to convert the stream into a string object:
// Declare char array Character[] chars = 'M', 'a', 'n', 'g', 'o'>; // Convert the char array to a string String str = Arrays.stream(chars) .map(String::valueOf) .collect(Collectors.joining()); System.out.println(str); // Mango
Read this article if you want to convert a string into a character array in Java. ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.