Java String toCharArray()

This java tutorial shows how to use the toCharArray() method of String class of java.lang package. This method returns a character array which corresponds to the character elements of our String.

Method Syntax :
public char[] toCharArray()
Parameter Input :
DataType Parameter Description
N/A N/A N/A
Method Returns :

This String toCharArray() method returns an array of characters which corresponds to the composition of our String in terms of characters equivalent.

Compatibility Version :
Since the beginning
Exception :
NullPointerException
Internal implementation
public char[] toCharArray() { // Cannot use Arrays.copyOf because of class //initialization order issues char result[] = new char[value.length]; System.arraycopy(value, 0, result, 0, value.length); return result; }

This can only be encountered if the String is null

Discussion :

The method toCharArray() is simply to converts our String object into array of characters. This is necessary especially if we needed to pass to a character array instead of String to java methods and API.

Supposed that we have the following string “Java String example”. If we want to convert it into a character array we would having the following:

String sampleString = "Java String Example"; char [] list = sampleString.toCharArray();

It’s pretty straightforward way to convert to character array. The string is broken down into set of characters, the first character to be assign on the first index of the character array which is index 0. Always remember that in dealing with arrays the general rule is first index would be 0;

As exercise, what do you think would be the contents of list[1] coming from the code snippet above? If you answer “J” that is incorrect. As i have said the index of an array begin at 0, so that would mean list[1] = “a”.

Java Code Example :

This example source code demonstrates the use of toCharArray() method of String class to converts a String object into character array. We have use the Arrays.toString method to print the value of our character array.

import java.util.Arrays; public class Test { public static void main(String[] args) { String str = "This is a sample string"; char[] Array = str.toCharArray(); System.out.println(Arrays.toString(Array)); } }


Output:
[T, h, i, s, , i, s, , a, , s, a, m, p, l, e, , s, t, r, i, n, g]
Java String.toCharArray() method example
public class Test { public static void main(String args[]) { String str = "cprogramcoding"; char[] Array = str.toCharArray(); for (char p: Array) { System.out.print(p + " "); } } }


Output:
c p r o g r a m c o d i n g
Java String toCharArray() method example
public class Test { public static void main(String args[]) { String s1="cprogramcoding"; char[] ch=s1.toCharArray(); for(int i=0;i<ch.length;i++) { System.out.print(ch[i]); } } }


Output:
cprogramcoding
Java String toCharArray() Method Example

See one more example of char array. It is useful method which returns char array from the string without writing any custom code.

public class Test { public static void main(String[] args) { String s1 = "Welcome to cprogramcoding"; char[] ch = s1.toCharArray(); int len = ch.length; System.out.println("Char Array length: " + len); System.out.println("Char Array elements: "); for (int i = 0; i < len; i++) { System.out.println(ch[i]); } } }


Output:
Char Array length: 25 Char Array elements: W e l c o m e t o c p r o g r a m c o d i n g



Instagram