How To Convert Char To String and a String to char in Java
文章推薦指數: 80 %
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
延伸文章資訊
- 1Convert a std::string to char* in C++ - Techie Delight
This post will discuss how to convert a `std::string` to `char*` in C++. The returned array shoul...
- 2如何在Java 中把一個字串轉換為字元Char | D棧- Delft Stack
本文介紹了在Java 中把String 轉換成char 的方法。
- 3Convert string to char array in C++ - GeeksforGeeks
A way to do this is to copy the contents of the string to char array. This can be done with the h...
- 4How To Convert Char To String and a String to char in Java
Converting String to Char. We can convert a String to char using charAt() method of String class....
- 5How to convert/parse from String to char in java?
If you want to parse a String to a char, whereas the String object represent more than one charac...