How to convert String to char in Java? Example

Earlier, we have seen how to convert a character to String in Java and today we'll learn opposite i.e. converting String to a character. Suppose, you have a String with value "s", how do you convert that to a char 's' in Java (FYI, string literal are quoted inside double quotes in Java e.g. "c" and character literals are quoted inside single quotes e.g. 'c' in J)? Well, if you know, Java String is made of a character array and java.lang.String class provides a method toCharArray() to retrieve that character array. If your String contains just one character then, the only element in character array is what you are looking after. Though that's not the only way to convert a String to char in Java.

You can also use charAt(index) method to get a char from String at a given index. Since String index starts from 0, "s".charAt(0) will return 's', which is even easier than earlier approach.

Let's see examples of both of these approaches to convert String to char in Java.



String to char using toCharArray()

The toCharArray() method returns the character array which makes the String. So, if you have just a single character as String e.g. "a" then the toCharArray() will return a character array with just one element e.g. array with length 1 and the first character would be the equivalent character for the String. For example, here is the code to convert "a" to character 'a' in Java:

String given = "a";
char[] chars = given.toCharArray();
char result = chars[0];


You can print both given String and character to see if they are same or not. Here is the output when I printed them into the console on my Eclipse IDE:

System.out.println("given string: " + given);
System.out.println("result character: " + result);

Output:
given string: a
result character: a

Btw, if you don't know System.out.println() method is overloaded and one version takes String while another take a character, here because we are concatenating String, the same method is called twice. See Complete Java Masterclass from Udemy to learn more about overloading and overriding in Java.

Java Program to convert String to character


String to char using charAt(index)

Whatever we have done in the previous example, can also be done by using the charAt(int index) method.

If you look closely, in the last example, we first get the character array and then retrieve the char from the first index because we knew that our String just got one character.

Instead of doing all this you could have just called the charAt(int index) method.

This method does same, i.e. return the character from the character array which backs the String. For example, if you have a String with just one character e.g. "b" then the following code will convert this into a character 'b':

String given = "b";
char b = given.charAt(0); 

This code will return the character from the first index, which is 'b', hence we have successfully converted string "b" to character 'b' in Java. You can further see, Introduction to Java for Programmers to learn more about String and character literals in Java.

Here is the screenshot of complete Java program and its output for your reference:

How to convert String to char in Java?

That's all about how to convert String to char in Java. This is one of the fundamental things which every Java developers should be aware of. You can solve many coding problems if you know these basic techniques because most of the String based coding problems are nothing but an array based problem, once you know how to convert a String to a character array.

Other String articles and interview questions for Java Developers
The Java Fundamentals: Java language by Jim Wilson
How to convert Map to List in Java?
How to replace characters and substring in given String?
How String in switch case works in Java?
How to convert Double to Long in Java?
What is the difference in String pool between Java 6 and 7?
How to convert Array to String in Java?

Thanks for reading this article so far. If you like this tutorial and my explanation then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

No comments:

Post a Comment