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

In char array to string program we will be using String(Char[] ch) constructor of Java String class. So, let’s see the example on char array to string java,

Example: char array to string java

Here’s the code for java char array to string,

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 character array to string java is: " + str);
   }
}

Output:

char array to string java


Using valueof method

Here we are going to see how to convert char array to string in java 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 java convert char array to string is: " + str);
   }
}

Output:

The java convert char array to string : FlowerBrackets


Java string to char array

Now let’s see java string to character 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("Character array to string java = " + Arrays.toString(charArray));
   }
}

Output:

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


conclusion

That’s it guys. This is all about how to write a program in java to convert a char array into string 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.

You May Also Like