Friday 10 August 2012

Convert String to Character Array Example


This example shows how to convert a given String object to an array of character using
toCharArray method of the String class.

public class StringToCharacterArrayExample {

 public static void main(String args[]) {

  // declare the String object
  String str = "Hello World";

  // declare the char array
  char[] stringArray;

  // convert string into array using toCharArray()
  // method of string class

  stringArray = str.toCharArray();
  System.out.println("The String array contains...");
  // display the array
  for (int index = 0; index < stringArray.length; index++)
   System.out.print(stringArray[index]);

 }

}


The output is:


The String array contains...
Hello World

No comments:

Post a Comment