Char Array to String Java

Hey guys!! Welcome to flower brackets blog. In this post we will learn and execute a simple program to convert char array to string java.

char array to string java

To convert a char to a string in java we will be creating string object and passing char array name to the constructor. Here’s the example,

Also Read – Convert Binary Number To Decimal Number in Java

Example:

public class CharArrayToString
{
   public static void main(String[] args)
   {
      char[] charArray = new char[]{'F','l','o','w','e','r','B','r','a','c','k','e','t','s'};

      String str = new String(charArray);
      System.out.println("Char array to string : " + str);
   }
}

Output:

Char array to string : FlowerBrackets

 


Convert character array to string

Here we are going to see how to convert a char array to a string in java using valueof method of String class,

public class CharArrayToStringDemo
{
   public static void main(String[] args)
   {
      char[] charArray = new char[]{'F','l','o','w','e','r','B','r','a','c','k','e','t','s'};
      
      String str = String.valueOf(charArray); 
      System.out.println("Java char array to string example : " + str);
   }
}

Output:

Java char array to string example : FlowerBrackets


Char array to string

Now let’s see how to convert char array to string using Character.toString method,

import java.util.Arrays;

public class CharArrayToStringDemo
{
   public static void main(String[] args)
   {
      char chArray = 'F';    
      String strArr = Character.toString(chArray);  
      System.out.println("Java char to string example : " + strArr);
   }
}

Output:

Java char to string example : F


Conclusion

That’s it guys. This is all about convert character array to string in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts