Javatpoint Logo
Javatpoint Logo

Java Convert String to char

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

Java Convert String to char

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.

Test it Now

Output:

1st character is: h

Let's see another example to convert all characters of a string into character.

Test it Now

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.

Test it Now

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




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA