Thursday 23 May 2019

Java: Convert String to char array


String class provides toCharArray method to get character array from string.

String str = "Hello World";
char[] charArray = str.toCharArray();

App.java
package com.sample.app;

public class App {
    public static void main(String args[]) {
        String str = "Hello World";

        char[] charArray = str.toCharArray();

        System.out.println("str : " + str);
        System.out.print("charArray : ");
        
        for(char ch : charArray) {
            System.out.print(ch + ",");
        }
    }
}

Output
str : Hello World

charArray : H,e,l,l,o, ,W,o,r,l,d,


You may like

No comments:

Post a Comment