How to Convert String to char in Java
How to Convert String to char in Java
There are two methods to convert String to char are:
- Using charAt() method
- Using tocharArray() method
Using charAt() method
This is the method of String class that belongs to java.lang package. It returns the character at the specified index value. The value of the index lies between 0 to length()-1 (where length() is the method of String class which returns the number of character in the string ). The signature of the method is given below:
public char charAt(int index)
Example
In the following example, str is a variable of String type that contains "Computer ". charAt() is a method of String classthat returns the character at the specified index. ch is a variable of type char that stores the returned character. The println statement prints the character return by the method.
public class StringtoCharExample { public static void main(String args[]) { String str="Computer"; char ch=str.charAt(2); //accessing character at index 2 i.e. m System.out.println("The second character of the String is:"+ch); } }
Output
The second character of the String is: m
If the index value is higher than string length or negative, then it throws IndexOutOfBoundsException. In the above example, string length is 7. If we specify the index value negative or higher, then it throws the IndexOutOfBoundsException.
char ch=str.charAt(9); //higher value char ch=str.charAt(-4); //negative value
Let’s see another example that returns each character.
public class StringtoCharExample1 { public static void main(String args[]) { String str="computer"; for(int i=0;i<str.length();i++) { char ch=str.charAt(i); System.out.println("The character " +ch+ is at index: "+i); } } }
Output
The character c is at index: 0 The character o is at index: 1 The character m is at index: 2 The character p is at index: 3 The character u is at index: 4 The character t is at index: 5 The character e is at index: 6 The character r is at index: 7
Using charArray() method
The tocharArray() method converts the String into the character array. It is a predefined method of String class that belongs to java.lang package. It returns the character array, which makes the String. If you want to get all character at a time, you can use a loop.
Example
In this example,”moon” is a string which we want to convert into the character array. char[] is an array that stores the array of characters. tocharArray() is a method that converts String to a character array.
In for loop, we have taken a variable i of type int that represents the index value of character. The variable i initialized with 0 because array index starts from 0. The test condition i<ch.length compares the value of i with the size of the array. i++ increments the value of i on each iteration until the test condition become false.
The println statement prints the character and its index value.
public class StringtoCharExample2 { public static void main(String args[]) { String str="moon"; char[] ch=str.toCharArray(); for(int i=0;i<ch.length;i++) { System.out.println("The character " +ch[i]+" is at index: "+i); } } }
Output
The character m is at index: 0 The character o is at index: 1 The character o is at index: 2 The character n is at index: 3
Remember
There is a slight difference between length() and length.
- length() is a method of the String class that returns the number of characters in the string. The length() method can be used only with strings not with arrays.
- The length is the property of array that determines the size of array. length can be used only with arrays not with strings.