All Posts

Java toCharArray() method

Java toCharArray() method

The Java toCharArray() method converts the given string to a character array. It returns a newly created character array, its length is similar to the string and its contents are initialized with the characters of this string.

Example

Let’s understand the usage of the toCharArray() method with the help of the below sample program:

package com.testingdocs.stringmethods;

public class ExampleProgram {

	public static void main(String[] args) {
		String str = "TestingDocs Java Tutorials";

		char[] chArr = str.toCharArray();
		System.out.println("Character Array of the String:");
		
        // Iterate over the character array elements
		for(int i=0;i < chArr.length;i++) {
			System.out.println(chArr[i]);
		}
	}
}

 

 

Java ToCharArray Method

Program Output

The output of the program is as shown below:

Character Array of the String:

T

e

s

t

i

n

g

D

o

c

s

J

a

v

a

T

u

t

o

r

i

a

l

s

Java Tutorials

Java Tutorial on this website: