Senin, 07 April 2014

How to convert Char to String in Java with Example



There are multiple ways to convert char to String in Java. In fact String is made of Character array in Java, which can be retrieved by calling String.toCharArray() method. Char is 16 bit or 2 byte unsigned data type in java mainly used to store characters. You can convert a single character into String for any purpose in Java by more than one ways. I thought about this article while writing how to convert Integer to String in Java but t it took little longer to get this post completed. In this Java tutorial we will see different ways to convert character into String in java with code Example.






4 ways to convert Char to String in Java



Here is list of 4 different ways I am aware of converting Character to String in Java , most of them are quite easy and doesn’t require much code but these Java examples are very helpful if you are new in Java programming.





Character to String Example 1: Character.toString



Character class provides a convenient toString() method which takes a character and return its String equivalent. toString() is static method in Character class and can be accessed using class name like Character.toString(). here is an example of converting char to String using Character.toString():






char ch = 'U';


String charToString = Character.toString(ch);






Character to String Example 2: String concatenation operator



In Java + can be used to concatenate String but it can also concatenate an String and a character and can result another String. This is another shortcut you can use to convert Character into String in Java. here is an example of converting character to String in Java:






char ch = 'U';


String str = "" + ch;






Character to String Example 3: using Anonymous Array



Anonymous array in Java can be used to wrap a single character into a char array and than passing that array into String constructor. a new String will be created from that character. see below for char to String example in Java:






char ch = 'U';


String fromChar = new String(new char[]{ch});






Character to String Example 4: using String.valueOf()



String.valueOf() is another neat and clean way of converting character into String in Java. I like this method


because its simple and straightforward or I say neat and clear. see following character to String conversion example in Java






char ch = 'U';


String valueOfchar = String.valueOf(ch);






Code Example of Converting Char to String in Java



This section contains complete code example of converting char to String in Java by using all four methods mentioned above. In my experience concatenation operator seems to be most popular but Character.toString() or String.valueOf() is my favorite way of converting a character to String object in java.






public class CharToStringExample {





       public static void main(String args[]) {


              char ch = 'U';





              // char to string using Character class


              String charToString = Character.toString(ch);


              System.out.println("Converting Char to String using Character class: " + charToString);





              // char to String using String concatenation


              String str = "" + ch;


              System.out.println("Converting Char to String using String concatenation: " + str);





              // char to String using anonymous array


              String fromChar = new String(new char[] { ch });


              System.out.println("Converting Char to String using anonymous array: " + fromChar);





              // char to String using String valueOf


              String valueOfchar = String.valueOf(ch);


              System.out.println("Converting Char to String using String valueOf: " + valueOfchar);





       }





}





Output:


Converting Char to String using Character class: U


Converting Char to String using String concatenation: U


Converting Char to String using anonymous array: U


Converting Char to String using String valueOf: U









That’s all on how to convert Char to String in Java with four different examples. For many of us its trivial operation but for many  beginners its quite helpful know various ways to change character into String.





Other Java String tutorial you may find interesting































Source:http://javarevisited.blogspot.com/2012/02/how-to-convert-char-to-string-in-java.html

Tidak ada komentar:

Posting Komentar