On this java tutorial, we will be discussing on how to convert a String to char array. There would be cases that the source value is a String object and our business requirements is to have a character array derived from the String input, thus this java example source code is made to to show the conversion of a string input to char array. Code is simple, and you might understand that after you go through this tutorial you will find it very easy. And that is site goal to assist you on enhancing your coding skills.
String to char array Conversion source code
This java source code shows how to convert a String to char array. The conversion is done by calling the toCharArray method of String class. This gives an output in character array format
package com.javatutorialhq.tutorial; import java.util.Arrays; /* * Java tutorial Example source code to convert Set to Array of Strings */ public class StringToCharArray { public static void main(String[] args) { // Input String declaration String input = "StringExampleConversion"; // convert the String to character array char[] charArray = input.toCharArray(); //Printing the contents of our character array System.out.println(Arrays.toString(charArray)); } }
Sample Output in the running the java program to convert String to char array
[S, t, r, i, n, g, E, x, a, m, p, l, e, C, o, n, v, e, r, s, i, o, n]
You might have already noticed that we have printed the contents of our char array in this example. You can go to how to print an array in java article to understand it further.