Convert Char to String And String to Char in Java

In your Java program you may have a requirement to convert a char to String or to convert a String to char. This post shows different options to do these conversions.

Convert char to String in Java

For converting char to String in Java there is a toString() method in the Character class.
If there is a Character class object then you can call toString() method on that object.
There is another static toString(char c) method which returns a String representation of the passed char.

Output

Another option for converting char to String in Java is to use String.valueOf(char c) method that returns the string representation of the char argument.

Output

Convert String to char in Java

For converting String to char in Java you can use of the following methods of the String class.

  • charAt(int index)– Returns the char value at the specified index.
  • toCharArray()– Converts this string to a new character array. Then from that array you can get the required char using the index.

Output

That’s all for the topic Convert Char to String And String to Char in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.