Char Array to String Java Program

Sharing is healthy !!

In this post we will learn and execute a simple program to convert char array to string java program.

In this program we will be using String(Char[] ch) constructor of Java String class. So, let’s see the example

Example: Char Array to String Java Program

Also Read – Convert Binary Number To Decimal Number in Java

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("The converted Char Array to String is: " + str);
   }
}

Output:

Char Array to String Java Program


using valueof method

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("The converted Char Array to String is: " + str);
   }
}

Output:

The converted Char Array to String is: FlowerBrackets


java string to char array

import java.util.Arrays;

public class StringToCharArray
{
   public static void main(String[] args)
   {
      String str = "flowerbrackets";
      // getting char at specific index
      char ind = str.charAt(2);
      // Character array from String
      char[] charArray = str.toCharArray();         
      System.out.println(str + " String index 2 character = " + ind); 
      System.out.println("Java string converted to character array = " + Arrays.toString(charArray));
   }
}

Output:

flowerbrackets String index 2 character = o
Java string converted to character array = [f, l, o, w, e, r, b, r, a, c, k, e, t, s]


conclusion

That’s it guys. This is all about character array to string and java string to char array java program. I hope you have understood the java program.

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

Do share this post if you like.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.