How to convert String to char AND String to char array in java



In this core java tutorial we will learn How to convert String to char in Java. We will also learn how to convert String to char array in Java with program and examples. Read when to convert to primitive type i.e int and wrapper classes i.e Integer in java.



HashMap Custom implementation in java - How HashMap works internally with diagrams and full program


How to convert String to char in Java >
  • Program/Example to convert String to char in Java (Most efficient way)


How to convert String to char array in Java >
  • Program/Example to convert String to char array in Java


Let’s convert String to char in Java
Program/Example to convert String to char in Java (Most efficient way)
We will convert string to char using charAt(index) method of String.
public class ConvertStringToCharExample1 {
   public static void main(String[] args) {
         
          String string = "a";

          //convert string to char using charAt method of String.
          char ch=string.charAt(0);
         
          //display char
          System.out.println("ch = "+ch);
   }
}
/*OUTPUT
ch = a
*/


Now, what about if string contains “abc” in place of “a”? output of above program will still remain same.
public class ConvertStringToCharExample1 {
   public static void main(String[] args) {
         
          String string = "abc";

          //convert string to char using charAt method of String.
          char ch=string.charAt(0);
         
          //display char
          System.out.println("ch = "+ch);
   }
}
/*OUTPUT
ch = a
*/

But, How to get all the characters of String? we will again use charAt(index) method of String, where we will change index.
public class ConvertStringToCharExample2 {
   public static void main(String[] args) {
         
          String string = "abc";
         
          //convert string to char using charAt method of String.
          char ch0=string.charAt(0);
          char ch1=string.charAt(1);
          char ch2=string.charAt(2);
         
         
          //display all char
          System.out.println("ch0 = "+ch0);
          System.out.println("ch1 = "+ch1);
          System.out.println("ch2 = "+ch2);
   }
}
/*OUTPUT
ch0 = a
ch1 = b
ch2 = c
*/


Now, let’s convert String to char array in Java


Program/Example to convert String to char array in Java
We will convert string to char using toCharArray() method of String.
public class ConvertStringToCharArrayExample3 {
   public static void main(String[] args) {
         
          String string = "abc";
         
          //convert string to char using charAt method of String.
          char charArray[]=string.toCharArray();
         
          //display all characters of Strings
          System.out.println("display all characters of Strings >");
          for(char ch : charArray){
                 System.out.println(ch);
          }
         
   }
}
/*OUTPUT
display all characters of Strings >
a
b
c
*/







Summary -
In this core java tutorial we learned conversion of String to char in Java. We also learned how to convert String to char array in Java with program and examples.

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

How to convert ArrayList to HashSet and Set to Array in Java examples

convert Array to Set and Set to array in Java





Collection - List, Set and Map all properties in tabular form





HashMap Custom implementation in java - How HashMap works internally with diagrams and full program


eEdit
Must read for you :