Convert string to char array in Java

Java 8Object Oriented ProgrammingProgramming

The following is our string.

String str = "Tutorial";

Now, use the toCharArray() method to convert string to char array.

char[] ch = str.toCharArray();

Now let us see the complete example.

Example

 Live Demo

public class Demo {
   public static void main(String []args) {
      String str = "Tutorial";
      System.out.println("String: "+str);
      char[] ch = str.toCharArray();
      System.out.println("Character Array...");
      for (int i = 0; i < ch.length; i++) {
         System.out.print(ch[i]+" ");
      }
   }
}

Output

String: Tutorial
Character Array...
T u t o r i a l
raja
Published on 13-Nov-2018 10:30:23
Advertisements