Monday, August 12, 2019

Converting String to Char Array in Java

This Java program shows how you can convert String to char array in Java. In Java String class there is a method toCharArray() that is used for the purpose. This method returns a character array whose length is the length of this string and the array is initialized to contain the character sequence represented by this string.

Convert String to char[] in Java

public class StringToCharArray {

 public static void main(String[] args) {
  String str = "Test String";
  // Converting to char array
  char[] charArray = str.toCharArray();
  // Accessing each character
  for(Character c : charArray)
   System.out.println(c);
 }
}

Output

T
e
s
t
 
S
t
r
i
n
g

That's all for this topic Converting String to Char Array in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Converting String to Byte Array - Java Program
  2. Converting Char to String And String to Char in Java
  3. How to Add Double Quotes to a String - Java Program
  4. Converting float to String in Java
  5. Converting Numbers to Words - Java Program

You may also like-

  1. Converting Enum to String in Java
  2. Binary Search Program in Java
  3. Shell Sort Program in Java
  4. How to Convert Date to String in Java
  5. Difference Between ArrayList And LinkedList in Java
  6. Exchanger in Java Concurrency
  7. Transaction Management in Spring
  8. Python String split() Method

No comments:

Post a Comment