Java – String toCharArray() method

In this article, we will discuss how to convert String into char[] array using String’s toCharArray() method

1. String’s toCharArray() method:

  • This String method is used to convert any given string into char[] array

1.1 Method Signature:

public char[] toCharArray();

1.2 Returns:

  • New char[] array equivalent to string’s length
  • Whose contents are initialized to contain the character sequence represented by the invoking string

2. Examples on toCharArray() method:

2.1 To create equivalent char[] array for the invoking string

StringToCharArrayMethod.java

package in.bench.resources.string.methods;

public class StringToCharArrayMethod {

	public static void main(String[] args) {

		// sample string
		String url = "JavaWorld";

		// convert string into char[] array
		char[] chArray = url.toCharArray();

		// iterate using for-loop and store into char[] array
		for(char ch : chArray) {
			System.out.println(ch);
		}
	}
}

Output:

J
a
v
a
W
o
r
l
d

Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String toLowerCase() method
Java - String substring() method