Java Program to Convert Character to String and Vice-Versa

Java Program to Convert Character to String and Vice-Versa

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to Convert Character to String and Vice-Versa.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

Char to String Conversion :

In Java, a char data type is a single 16-bit Unicode character, while a String is an object that represents a sequence of characters. To convert a char to a String, you can use the Character.toString(char c) method or the String.valueOf(char c) method. Both of these methods take a single char as an argument and return a String containing that char.

Program to Convert Character to String Using toString Function:

Run
public class Main {
    public static void main(String[] args) {
        // Initializing a Char value
        char c = 'a';

        // Converting char to string using toString Function
        String s = Character.toString(c);

        // Printing the character value
        System.out.println("Character: " + c);

        // Printing the String Value
        System.out.println("String: " + s);
    }
}
Output:

Character: a
String: a

Program to Convert Character to String Using + Operator :

Run
public class Main {
    public static void main(String[] args) {
        // Initializing a Char value
        char c = 'a';

        // Converting char to string using + Operator
        String s = "" + c;

        // Printing the character value
        System.out.println("Character: " + c);

        // Printing the String Value
        System.out.println("String: " + s);
    }
}
Output:

Character: a
String: a

Program to Convert String to Character Using charAt Function :

Run
public class Main {
    public static void main(String[] args) {
        // Initializing a String value
        String s = "a";

        // Converting String to char using charAt Function
        char c = s.charAt(0);

        // Printing the String value
        System.out.println("String: " + s);

        // Printing the Character Value
        System.out.println("Character: " + c);
    }
}
Output:

String: a
Character: a

Program to Convert String to Character Using toCharArray Function :

Run
public class Main {
    public static void main(String[] args) {
        // Initializing a String value
        String s = "a";

        // Converting String to char using toCharArray Function
        char c = s.toCharArray()[0];

        // Printing the String value
        System.out.println("String: " + s);

        // Printing the Character Value
        System.out.println("Character: " + c);
    }
}
Output:

String: a
Character: a

It’s important to note that if the string is empty or contains more than one character, it will throw an exception StringIndexOutOfBoundsException as charAt() method returns the character at the specified index. So, it’s better to check the length of the string before calling this method.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription