Tuesday, March 19, 2024

How to convert String to char in Java? Example Tutorial

Hello Java Programmers, if you are wondering how to convert a String to char value in Java then you have come to the right place. Earlier, we have seen how to convert a character to String in Java and today we'll learn opposite i.e. converting String object to a character value. You know the difference right? String is an Object while char is a primitive value. So here we are also going to convert a Java object into a primitive value. By the way, this conversion only make sense if String is a single character. For example, 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, like "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() method

The toCharArray() method returns the character array which makes the String. So, if you have just a single character as String like "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 takes 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 these free Java Online Courses for Beginners 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 I think every Java developers should be aware of. Although this works only for single character String, 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 problems, once you know how to convert a String to a character array.

Other String articles and interview questions for Java Developers

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

Feel free to comment, ask questions if you have any doubt.