How To Convert Char To String and a String to char in Java

文章推薦指數: 80 %
投票人數:10人

Converting String to Char. We can convert a String to char using charAt() method of String class. class StringToCharDemo { public static void main(String args ... Inthistutorial,wewillseeprogramsforchartoStringandStringtocharconversion. ProgramtoconvertchartoString WehavefollowingtwowaysforchartoStringconversion. Method1:UsingtoString()method Method2:UsngvalueOf()method classCharToStringDemo { publicstaticvoidmain(Stringargs[]) { //Method1:UsingtoString()method charch='a'; Stringstr=Character.toString(ch); System.out.println("Stringis:"+str); //Method2:UsingvalueOf()method Stringstr2=String.valueOf(ch); System.out.println("Stringis:"+str2); } } Output: Stringis:a Stringis:a ConvertingStringtoChar WecanconvertaStringtocharusingcharAt()methodofStringclass. classStringToCharDemo { publicstaticvoidmain(Stringargs[]) { //UsingcharAt()method Stringstr="Hello"; for(inti=0;i



請為這篇文章評分?