Given a string, the task is to convert this string into a character array in Java.
Examples:
Input: Hello World Output: [H, e, l, l, o,, W, o, r, l, d] Input: GeeksForGeeks Output: [G, e, e, k, s, F, o, r, G, e, e, k, s]
-
Method 1: Naive Approach
- Step 1: Get the string.
- Step 2: Create a character array of the same length as of string.
- Step 3: Traverse over the string to copy character at the i’th index of string to i’th index in the array.
- Step 4: Return or perform the operation on the character array.
Below is the implementation of the above approach:
// Java program to Convert a String// to a Character array using Naive Approachimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){String str ="GeeksForGeeks";// Creating array of string lengthchar[] ch =newchar[str.length()];// Copy character by character into arrayfor(inti =0; i < str.length(); i++) {ch[i] = str.charAt(i);}// Printing content of arrayfor(charc : ch) {System.out.println(c);}}}chevron_rightfilter_noneOutput:G e e k s F o r G e e k s
-
Method 2: Using toCharArray() Method
- Step 1:Get the string.
- Step 2:Create a character array of same length as of string.
- Step 3:Store the array return by toCharArray() method.
- Step 4:Return or perform operation on character array.
// Java program to Convert a String// to a Character array using toCharArray()importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){String str ="GeeksForGeeks";// Creating array and Storing the array// returned by toCharArray()char[] ch = str.toCharArray();// Printing arrayfor(charc : ch) {System.out.println(c);}}}chevron_rightfilter_noneOutput:G e e k s F o r G e e k s
Recommended Posts:
- Java Program to convert Character Array to IntStream
- Convert Set of String to Array of String in Java
- Convert an ArrayList of String to a String array in Java
- Difference between String and Character array in Java
- Convert a Set of String to a comma separated String in Java
- Convert a List of String to a comma separated String in Java
- Program to convert first character uppercase in a sentence
- Queries to find the first non-repeating character in the sub-string of a string
- How to convert an Array to String in Java?
- Java Program to get a character from a String
- Java program to count the occurrence of each character in a string using Hashmap
- Replace a character at a specific index in a String in Java
- How to find the first and last character of a string in Java
- Remove first and last character of a string in Java
- Swap the first and last character of a string in Java
- How to get character array from string in JavaScript?
- Program to convert List of Integer to List of String in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Program to convert List of String to List of Integer in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

