Java String toCharArray() Method With Example Last Updated : 29 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the toCharArray() method of the String class converts the given string into a character array. The returned array length is equal to the length of the string. This is helpful when we need to work with individual characters of a string for tasks like iteration or modification.Example:Below is a basic example that demonstrates the toCharArray() method of String class. Java // Java program to demonstrate toCharArray() method public class ToCharArray { public static void main(String[] args) { // Define a string String s = "Java"; // Convert the string to a character array char[] ca = s.toCharArray(); System.out.println(ca); } } OutputJava Explanation: In the above example, the string "Java" is converted into a character using toCharArray() method.Syntax of toCharArray()public char[] toCharArray();Return Value: It returns a newly created character array containing the characters of the string.Example of String toCharArray() MethodHere is another example, where we will be using the toCharArray() method and print each character individually. Java // Java program to print individual // characters of a string // Using toCharArray() public class ToCharArray1 { public static void main(String[] args) { // Define a string String s = "GeeksForGeeks"; // Convert the string to a character array char[] ca = s.toCharArray(); for (char ch : ca) { System.out.println(ch); } } } OutputG e e k s F o r G e e k s Explanation: In the above example, the toCharArray() method breaks the string "GeeksForGeeks" into individual characters for processing. Then a for-each loop iterates through the array and prints each character on a new line. Comment More infoAdvertise with us Next Article Java String toCharArray() Method With Example N Niraj_Pandey Follow Improve Article Tags : Java DSA Java-Strings Java-lang package Java-Functions +1 More Practice Tags : JavaJava-Strings Similar Reads Stack toArray() method in Java with Example The toArray() method of Stack class in Java is used to form an array of the same elements as that of the Stack. Basically, it copies all the element from a Stack to a new array. Syntax: Object[] arr = Stack.toArray() Parameters: The method does not take any parameters. Return Value: The method retur 2 min read Java Guava | Shorts.toArray() method with Examples The toArray() method of Shorts Class in the Guava library is used to convert the short values, passed as the parameter to this method, into a Short Array. These short values are passed as a Collection to this method. This method returns a Short array. Syntax: public static short[] toArray(Collection 2 min read SortedSet toArray() method in Java with Examples The toArray() method of Java SortedSet is used to form an array of the same elements as that of the SortedSet. Basically, it copies all the element from a SortedSet to a new array. Syntax: Object[] toArray() Parameters: The method does not take any parameters. Return Value: The method returns an arr 2 min read HashSet toArray(T[]) method in Java with Example The toArray(T[]) method method of HashSet class in Java is used to form an array of the same elements as that of the HashSet. It returns an array containing all of the elements in this HashSet in the correct order; the run-time type of the returned array is that of the specified array. If the HashSe 4 min read TreeSet toArray(T[]) method in Java with Example The toArray(T[]) method method of TreeSet class in Java is used to form an array of the same elements as that of the TreeSet. It returns an array containing all of the elements in this TreeSet in the correct order; the run-time type of the returned array is that of the specified array. If the TreeSe 4 min read StringBuffer toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read Vector toArray() Method in Java with Examples 1. toArray() The toArray() method of Vector class in Java is used to form an array of the same elements as that of the Vector. Basically, it copies all the element from a Vector to a new array. Syntax: Object[] arr = Vector.toArray() Parameters: The method does not take any parameters. Return Value: 5 min read HashSet toArray() method in Java with Example The toArray() method of Java HashSet is used to form an array of the same elements as that of the HashSet. Basically, it copies all the element from a HashSet to a new array. Syntax: Object[] arr = HashSet.toArray() Parameters: The method does not take any parameters. Return Value: The method return 2 min read TreeSet toArray() method in Java with Example The toArray() method of Java TreeSet is used to form an array of the same elements as that of the TreeSet. Basically, it copies all the element from a TreeSet to a new array. Syntax: Object[] arr = TreeSet.toArray() Parameters: The method does not take any parameters. Return Value: The method return 2 min read AbstractSet toArray(T[]) method in Java with Example The toArray(T[]) method method of AbstractSet class in Java is used to form an array of the same elements as that of the AbstractSet. It returns an array containing all of the elements in this AbstractSet in the correct order; the run-time type of the returned array is that of the specified array. I 4 min read Like