Java String toCharArray() method with examples

toCharArray() converts a string to a new character array. In this post, we will learn in detail about the toCharArray() method of the String class with some what-if scenarios.

  • Method declaration – public char[] toCharArray()
  • What does it do? It will convert a string into a new character array with the same length as the string and contain elements represented by the character sequence of the string. Afterward, we can perform array operations on the resulting array.
  • What does it return? It will return a char array( char[] )
toCharArray() method usage in strings
Code Example
public class Codekru {

	public static void main(String[] args) {

		String str = "codekru";
		int lengthOfString = str.length();

		char[] charArray = str.toCharArray(); // returning a character array
		int lengthOfCharArray = charArray.length; // to find the length of the array

		System.out.println("Length of the actual string: " + lengthOfString);
		System.out.println("Length of the returned char array: " + lengthOfCharArray);

		System.out.println("Characters within the char array:");
		for (int i = 0; i < lengthOfCharArray; i++) {
			System.out.print(charArray[i] + " ");
		}

	}

Output –

Length of the actual string: 7
Length of the returned char array: 7
Characters within the char array:
c o d e k r u 
Time complexity of the toCharArray() method

toCharArray() copies every character of the string into the array. So, the time complexity of the toCharArray() will be O(n), where n is the number of characters in the string.

The What If scenarios

Q – What if we used toCharArray() on a null string?

We will get a NullPointerException.

public class Codekru {

	public static void main(String[] args) {

		String str = null;

		char[] charArray = str.toCharArray(); // returning a character array
		int lengthOfCharArray = charArray.length; // to find the length of the array

		System.out.println("Characters within the char array:");
		for (int i = 0; i < lengthOfCharArray; i++) {
			System.out.print(charArray[i] + " ");
		}

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toCharArray()" because "str" is null
Q – What if we used toCharArray() on a string containing a newline character?

Newline character(\n) will also be there in the char array, as illustrated by the program below.

public class Codekru {

	public static void main(String[] args) {

		String str = "codekru\nwebsite"; // string with a newline characters

		char[] charArray = str.toCharArray(); // returning a character array
		int lengthOfCharArray = charArray.length; // to find the length of the array

		System.out.println("Characters within the char array:");
		for (int i = 0; i < lengthOfCharArray; i++) {
			System.out.print(charArray[i] + " ");
		}

	}
}

Output –

Characters within the char array:
c o d e k r u 
 w e b s i t e 

Please visit this article to learn more about the java strings and their methods.

We hope that you liked the article. If you have any doubts or concerns, please feel free to reach us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *