String To Char Array Java

String To Char Array Java | In this blog, we will convert a string into a character array, the conversion happens between a primitive data type and a non-primitive data type where the string is the non-primitive type and the character belongs to the primitive data type.

Example for Java String To Char Array:-
1. String = β€œHello”
Character array = [β€˜H’, β€˜e’, β€˜l’, β€˜l’, β€˜o’]
2. String = β€œKnow Program”
Character array = [β€˜K’, β€˜n’, β€˜o’, β€˜w’, β€˜ β€˜ , β€˜P’, β€˜r’, β€˜o’, β€˜g’, β€˜r’, β€˜a’, β€˜m’]

There are different ways to convert a string to an array of chars in Java:-
1. By using a Naive approach
2. By using the String.toCharArray() Method

String To Array Of Chars in Java

Now let us first concentrate on the Naive approach. For this, we first need to get a string and then create an array of characters of the exact length of the string. Then we need to traverse over the string to copy the ith element of the string to the character array. Finally, return the array of characters.

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      String string = "Know Program";
      char[] charArray = toCharArray(string);
      System.out.println("Char array: " + Arrays.toString(charArray));
   }

   public static char[] toCharArray(String string) {
      char[] ch = new char[string.length()];
      for (int i = 0; i < string.length(); i++) {
         ch[i] = string.charAt(i);
      }
      return ch;
   }
}

Output:-

Char array: [K, n, o, w, , P, r, o, g, r, a, m]

In the above program to convert string to char array in Java, we have defined the toCharArray() method where an array is initialized with the same length of the string. Then we iterated through the string and fetched each character using the String.charAt() method. Characters are stored in the array and returned to the caller method.

In the main method to display the array of chars, we have used Arrays.toString() method. But you can also use for loop, for-each loop, while loop e.t.c. See more:- Different ways to print array in Java. Also see:- Char Array To String Java

String To Char Array in Java using String.toCharArray()

Java String class also provides the toCharArray() method to convert string to char array. Therefore instead of creating our own method, we can directly call String.toCharArray() method and it will convert the given string into an array of chars.

Java program to demonstrate how to convert a string into a char array in Java using the String.toCharArray() method.

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      String string = "Java Programming Language";
      char[] charArray = string.toCharArray();
      System.out.println("Char array: " + Arrays.toString(charArray));
   }
}

Output:-

Char array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.