Previous Page

Convert a string to a character array - Java short program

Copy and Paste the following code into your favorite Java Integrated Development Environment (IDE) - compile and run.

This is an easy way to convert a string to a character array. This short program outputs a couple of examples of the new array, also.

// Description: Converts a string to a character array

public class sentenceToCharacterArray
	public static void main(String[] args)
		String sentence = "Welcome to!";
		char[] charStr = sentence.toCharArray(); // The string "sentence" is stored in the "charStr" array 
		System.out.println(charStr[4]);  // output 4th index of the array
		System.out.print(charStr[11]); // output 11th index of the array