
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- How can we convert character array to a Reader in Java?
- How can we convert a JSONArray to String Array in Java?
- In how many ways can we split a string in JavaScript?
- Java program to convert a character array to string
- 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 JSON array to a list using Jackson in Java?\n
- In how many ways we can concatenate Strings in Java?
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- How can we convert a JSON string to a JSON object in Java?
- How many ways are there to convert an Array to ArrayList in Java?
- How can we convert a list to the JSON array in Java?
- Can we convert a list to an Array in Java?
- How do you convert a string to a character array in JavaScript?
