How To Convert Char To String and a String to char in Java


n this tutorial, we will see programs for char to String and String to char conversion.

Program to convert char to String

We have following two ways for char to String conversion.
Method 1: Using toString() method
Method 2: Usng valueOf() method

class CharToStringDemo
   public static void main(String args[])
      // Method 1: Using toString() method
      char ch = 'a';
      String str = Character.toString(ch);
      System.out.println("String is: "+str);
      // Method 2: Using valueOf() method
      String str2 = String.valueOf(ch);
      System.out.println("String is: "+str2);

String is: a
String is: a
Converting String to Char

We can convert a String to char using charAt() method of String class.

class StringToCharDemo
   public static void main(String args[])
      // Using charAt() method
      String str = "Hello";
      for(int i=0; i         char ch = str.charAt(i);
        System.out.println("Character at "+i+" Position: "+ch);

Character at 0 Position: H
Character at 1 Position: e
Character at 2 Position: l
Character at 3 Position: l
Character at 4 Position: o

asked Jun 11, 2015 in Java Interview Questions by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.