Java - String toCharArray() Method



The Java String toCharArray() method is used to convert the current string to a character array. This method returns a newly formed character array with a length equal to the given string and with the characters in the given string initialized as its contents.

For example, the string “Tutorials Point” is converted into a new character array “Tutorials Point” where the size of the character is 15 (spaces are included).

Note − Unlike C, in Java, a character array is distinct from a string array and the NULL character cannot terminate either a string or a character array.

Syntax

Following is the syntax for Java String toCharArray() method −

public char[] toCharArray()

Parameters

This method does not accept any parameter.

Return Value

This method returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Example

The following example shows the usage of Java String toCharArray() method by printing the character array of the given string value " Java was developed by James Gosling" −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      
      // converts String value to character array type value  
      String str = " Java was developed by James Gosling";
      char retval[] = str.toCharArray();    
      
      // displays the converted value     
      System.out.println("Converted value to character array = ");
      System.out.println(retval);
   }
}

Output

If you compile and run the above program, it will produce the following result −

Converted value to character array =
Java was developed by James Gosling

Example

Following is an example to print the size of the character array of the given string "Tutorials point!" using toCharArray() method −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      String s = new String("Tutorials point!");
      char[] array = s.toCharArray();
      System.out.print("The string i:");
      System.out.println(s.toCharArray());
      System.out.println("Size of char is: " + array.length); 
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

The string i:Tutorials point!
Size of char is: 16

Example

Let's examine one more char array example. Without having to write any custom code, it is a useful method that returns a char array from the string −

public class StringDemo {
   public static void main(String[] args) {
      String s = "Programming";
      char[] c = s.toCharArray();
      System.out.println("Size of char array: " + c.length);
      System.out.println("Elements of character array: ");
      for (int i = 0; i < c.length; i++) {
         System.out.println(c[i]);
      }
   }
}

Output

On executing the program above, the output is obtained as follows −

Size of char array: 11
Elements of character array:
P
r
o

g
r
a
m
m
i
n
g

Example

Below is the code to return the character array of the string value passed as an argument by iterating over the string through a loop −

public class StringDemo {
   public static void main(String args[]) {
      String s = "Tutorials Point";
      char[] c = s.toCharArray();
      int l = c.length;
      for (int i = 0; i < l; i++) {
         System.out.print(c[i]);
      }
   }
}

Output

The output of the above program is as follows −

Tutorials Point
java_lang_string.htm
Advertisements