In this tutorial, we will learn how to convert string to char data type in Java easily.
There are mainly two ways for converting a string into char data type in Java. They are as:
- Convert using charAt() method
- Convert using toCharArray() method
To convert string to a char data type in Java, we use charAt() method of String class. The general signature of charAt() method is as below:
public char charAt(int index)
This method takes an integer value, which represents an index number. The index number ranges from 0 to length() – 1. The charAt() method returns the character present at the specified index number in a string.
In other words, we can use charAt() method to extract a character from a specific position in a string. The first character (or char value) of the string is at index number 0, the next at index number 1,and so on, as for array indexing.
For example:
String str = "hello"; char ch = str.charAt(1); // It will return character e.
Since the charAt() method returns a single character only, therefore, to get all characters, you need to use loop.
Let’s make a simple Java program to converting a string into char data type.
Program code 1:
package stringPrograms; public class StringToChar { public static void main(String[] args) { // Create a string literal. String str = "Hello"; // Call charAt() method of String class to convert string to char. char ch = str.charAt(2); // index numbers begin at zero. System.out.println("Character at index number 2: " +ch); } }
Output: Character at index number 2: l
Let’s take another example program to convert all characters of a string into character using for loop.
Program code 2:
package stringPrograms; public class StringToChar { public static void main(String[] args) { // Create a string literal. String str = "Hello"; for(int i = 0; i < str.length(); i++) { // Call charAt() method of String class to convert string to char. char ch = str.charAt(i); System.out.println("Character at index "+i+": " +ch); } } }
Output: Character at index 0: H Character at index 1: e Character at index 2: l Character at index 3: l Character at index 4: o
Converting String to Char in Java using toCharArray() method
Let’s create a Java program to convert string to char data type using toCharArray() method. The toCharArray() method of String class converts the string into a new character array.
The general syntax to declare toCharArray() method of String class is as:
char[ ] String toCharArray()
This method does not take any parameter. It returns a newly allocated character array whose length is the length of this string.
Program code 3:
package stringPrograms; public class StringToChar { public static void main(String[] args) { // Create a string literal. String str = "Scientech"; char[ ] ch = str.toCharArray(); for(int i = 0; i < ch.length; i++) { System.out.println("char at index "+i+": "+ch[i]); } } }
Output: char at index 0: S char at index 1: c char at index 2: i char at index 3: e char at index 4: n char at index 5: t char at index 6: e char at index 7: c char at index 8: h
In this tutorial, you learned how to convert a string to char data type in Java in two ways easily. Hope that you will have understood the basic concepts for the conversion of a string into character and practiced all programs.
Thanks for reading!!!
Next ⇒ Convert Char to String in Java⇐ Prev Next ⇒