The charAt() method returns a single character of specified index. The signature of charAt() method is given below:
public char charAt(int index)
Java String to char Example: charAt() method
Let's see the simple code to convert String to char in java using charAt() method.
String s="hello";
char c=s.charAt(0);//returns h
Let's see the simple example of converting String to char in java.
public class StringToCharExample1{
public static void main(String args[]){
String s="hello";
char c=s.charAt(0);//returns h
System.out.println("1st character is: "+c);
}}
Output:
1st character is: h
Let's see another example to convert all characters of a string into character.
public class StringToCharExample2{
public static void main(String args[]){
String s="hello";
for(int i=0; i<s.length();i++){
char c = s.charAt(i);
System.out.println("char at "+i+" index is: "+c);
}
}}
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.
public class StringToCharExample3{
public static void main(String args[]){
String s1="hello";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.println("char at "+i+" index is: "+ch[i]);
}
}}
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
Signature
The charAt() method returns a single character of specified index. The signature of charAt() method is given below:
public char charAt(int index)
Java String to char Example: charAt() method
Let's see the simple code to convert String to char in java using charAt() method.
String s="hello";
char c=s.charAt(0);//returns h
Let's see the simple example of converting String to char in java.
Output:
Let's see another example to convert all characters of a string into character.
Output:
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: