Skip to Content

Convert String to Character Array Example

  1. /*
  2.  Convert String to Character Array Example
  3.  This example shows how to convert a given String object to an array
  4.  of character
  5.  */
  6.  
  7. public class StringToCharacterArrayExample {
  8.  
  9.   public static void main(String args[]){
  10.     //declare the original String object
  11.     String strOrig = "Hello World";
  12.     //declare the char array
  13.     char[] stringArray;
  14.    
  15.     //convert string into array using toCharArray() method of string class
  16.     stringArray = strOrig.toCharArray();
  17.    
  18.     //display the array
  19.     for(int index=0; index < stringArray.length; index++)
  20.       System.out.print(stringArray[index]);
  21.    
  22.   }
  23.  
  24. }
  25.  
  26. /*
  27. Output of the program would be :
  28. Hello World
  29. */