Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

How to convert String to char and vice versa?

Category: Java

This is a tricky question because String is a sequence of characters, so we can’t convert it to a single character. We can use use charAt method to get the character at given index or we can use toCharArray() method to convert String to character array.

import java.util.Arrays;

public class StringChar {

public static void main(String[] args) {
    String st = "This is great";

    char[] chars = st.toCharArray();
    System.out.println(Arrays.toString(chars));
}

}

Output

[T, h, i, s, , i, s, , g, r, e, a, t]

Leave a Reply

Your email address will not be published. Required fields are marked *