
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In how many ways we can convert a String to a character array using Java?
You can convert a String to a character array either by copying each element of the String to an array or, using the toCharArray() method.
Copying each element
Get the String to be converted.
Create an empty character array with the length of the String.
The charAt() method of the String class returns the character at a particular position. Using this method copy each character of the String to the array.
Example
import java.util.Arrays; import java.util.Scanner; public class StringToCharArray { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String value: "); String str = sc.next(); //Creating an empty array with the length of the String char chArray[] = new char[str.length()]; //Copying each element of the String to the array for(int i=0; i<str.length(); i++) { chArray[i] = str.charAt(i); } System.out.println("Contents of the character array: "); System.out.println(Arrays.toString(chArray)); } }
Output
Enter a String value: Tutorialspoint Contents of the String array: [T, u, t, o, r, i, a, l, s, p, o, i, n, t]
Using toCharArray() method
The toCharArray() method of the Strong class converts the current String into a character array and returns it. Therefore, to convert a Sting into a character array using this method −
Get the String to be converted.
Create an empty character array with the length of the String.
Convert the String to character array using the toCharArray() method and store it in the above created empty array.
Example
import java.util.Arrays; import java.util.Scanner; public class ToCharArrayExample { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String value: "); String str = sc.next(); //Creating an empty array with the length of the String char chArray[] = str.toCharArray(); System.out.println("Contents of the character array: "); System.out.println(Arrays.toString(chArray)); } }
Output
Enter a String value: Tutorialspoint Contents of the character array: [T, u, t, o, r, i, a, l, s, p, o, i, n, t]
- Related Questions & Answers
- In how many ways can we split a string in JavaScript?
- How can we convert character array to a Reader in Java?
- How many ways a String object can be created in java?
- In how many ways can we find a substring inside a string in javascript?
- How can we convert a JSONArray to String Array in Java?
- In how many ways we can concatenate Strings in Java?
- Java program to convert a character array to string
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- How can we convert a JSON array to a list using Jackson in Java?
- How many ways are there to convert an Array to ArrayList in Java?
- How many ways can we read data from the keyboard in Java?
- How can we convert a JSON string to a JSON object in Java?
- How many ways to iterate a TreeSet in Java?
- How many ways to iterate a LinkedList in Java?