- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to create Character Array from String Objects
Use the toCharArray() method in Java to create character arrays from string objects. The following is our string.
String str = "This is it!";
Now, let us create character array from the above string.
char[] strArr = str.toCharArray();
Example
public class Demo { public static void main(String[] args) { String str = "This is it!"; char[] strArr = str.toCharArray(); for(char res: strArr) { System.out.println(res); } } }
Output
T h i s i s i t !
Let us see another example in which we are creating string using new operator.
Example
public class Demo { public static void main(String[] args) { String str = new String("This is another string!"); char[] strArr = str.toCharArray(); for(char res: strArr) { System.out.println(res); } } }
Output
T h i s i s a n o t h e r s t r i n g !
Advertisements