How to Convert String to Character Array in Java


Here we may learn how to convert a given String object to an array of character in Java.

Source Code

package com.beginner.examples;

public class StringToCharacterArray {

	public static void main(String[] args) {
		
		String string= "Hello World";
		
		char[] stringArray;
		
		System.out.println("Before converting to stringArray , string is : " + string);
		/*
		 * To convert string into array use toCharArray() method of string.
		 */
		stringArray = string.toCharArray(); 
		
		
		System.out.print("After converting to stringArray , string is : ");
		for(int index=0; index < stringArray.length; index++) {
			System.out.print(stringArray[index]);
		}
		
	}

}

Output:

Before converting to stringArray , string is : Hello World
After converting to stringArray , string is : Hello World
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Convert String to Character Array in Java


In this example, we will learn how to turn a string into a character array in Java.

Source Code

package com.beginner.examples;

public class StringToArrayCharExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "I love you";
		
		char[] chs = str.toCharArray();
		
		for(int i=0; i < chs.length; i++)
		{
			System.out.print(chs[i]);
			
		}
	}

}

Output:

I love you
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments