80
Java Convert String to char
We can convert String to char in java using charAt() method of String class.
The charAt() method returns a single character only. To get all characters, you can use loop.
Signature
The charAt() method returns a single character of specified index. The signature of charAt() method is given below:
Java String to char Example: charAt() method
Let’s see the simple code to convert String to char in java using charAt() method.
Let’s see the simple example of converting String to char in java.
Output:
1st character is: h
Let's see another example to convert all characters of a string into character.
Output:
char at 0 index is: h char at 1 index is: e char at 2 index is: l char at 3 index is: l char at 4 index is: o
Java String to char Example: toCharArray() method
Let's see the simple code to convert String to char in java using toCharArray() method. The toCharArray() method of String class converts this string into character array.
Output:
char at 0 index is: h char at 1 index is: e char at 2 index is: l char at 3 index is: l char at 4 index is: o
Next TopicJava char to String