Table of Contents
There are multiple ways to convert Char Array to String in java.
When you run above program, you will get below output
Some of them are:
- Using String class’s constructor (By passing Char array to contructor)
- Using String class’s valueOf method
- Using iterating char array
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; public class CharArrToStringMain { /* * @ Author : Arpit Mandliya */ public static void main(String args[]) { char[] charArr = { 'J', 'a', 'v', 'a', '2', 'b', 'l', 'o', 'g' }; // Using String's constructor System.out.println("--------------------"); System.out.println("Using String's contructor"); System.out.println("--------------------"); String str = new String(charArr); System.out.println(str); System.out.println(); // Using String's valueOf method System.out.println("--------------------"); System.out.println("Using String's valueOf"); System.out.println("--------------------"); String strV = String.valueOf(charArr); System.out.println(strV); System.out.println(); // Using iterating char Array System.out.println("--------------------"); System.out.println("Using iterating char Array"); System.out.println("--------------------"); String strT = ""; for (int i = 0; i < charArr.length; i++) { strT += charArr[i]; } System.out.println(strT); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-------------------- Using String's contructor -------------------- Java2blog -------------------- Using String's valueOf -------------------- Java2blog -------------------- Using iterating char Array -------------------- Java2blog |
Other String Programs are :
- How to convert String to char Array in java
- How to reverse String in java
- Find length of String without using java inbuilt length method
- Find all substrings of String in java
- Find First non repeated character in a String
- Java Program to check Palindrome String
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.