Write a Java Program to Convert Character to String and Vice-Versa

Java Program to Convert Character to String and Vice-Versa

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Convert Character to String and Vice-Versa
Java Program to Convert Character to String and Vice-Versa

Write a Java Program to Convert Character to String and Vice-Versa

PROGRAM: Java Program to Convert Character to String and Vice-Versa

/*Java Program to Convert Character to String and Vice-Versa*/

1: Convert char to String

public class CharString {

    public static void main(String[] args) {
        char ch = 'c';
        String st = Character.toString(ch);
        // Alternatively
        // st = String.valueOf(ch);

        System.out.println("The string is: " + st);
    }
}

2: Convert char array to String

public class CharString {

    public static void main(String[] args) {
        char[] ch = {'a', 'e', 'i', 'o', 'u'};

        String st = String.valueOf(ch);
        String st2 = new String(ch);

        System.out.println(st);
        System.out.println(st2);
    }
}

 3: Convert String to char 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));
    }
}

Leave a Reply

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