分享

java中String类型转换方法

 tracyf 2014-02-13

integer   to   String   :
int     42;
String   str    Integer.toString(i);
or
String   str    ""    i
  
double   to   String   :
String   str    Double.toString(i);
  
long   to   String   :
String   str    Long.toString(l);

float   to   String   :
String   str    Float.toString(f);

String   to   integer   :
str    "25";
int     Integer.valueOf(str).intValue();  
or  
int     Integer.parseInt(str);  

String   to   double   :
double     Double.valueOf(str).doubleValue();
  
String   to   long   :
long     Long.valueOf(str).longValue();
or
long     Long.parseLong(str);
 
String   to   float   :
float     Float.valueOf(str).floatValue();

decimal   to   binary   :
int     42;
String   binstr    Integer.toBinaryString(i);

decimal   to   hexadecimal   :
int     42;
String   hexstr    Integer.toString(i,   16);
or
String   hexstr    Integer.toHexString(i);

hexadecimal   (String)   to   integer   :
int     Integer.valueOf("B8DA3",   16).intValue();
or
int     Integer.parseInt("B8DA3",   16);

ASCII   code   to      64;
String   aChar    new   Character((char)i).toString();

integer   to   ASCII   code      'A';  
int     (int)   c;   //    will   have   the   value   65   decimal 

To   extract   Ascii   codes   from     test    "ABCD";
for    int     0;     test.length();   ++i    {
char     test.charAt(    );
int     (int)   c;
System.out.println(i);
 
   
integer   to   boolean  :
   (i   !=   0);  

boolean   to    

note   :
To   catch   illegal   number   conversion,   try   using   the   try/catch   mechanism.
try{
   Integer.parseInt(aString);
}
catch(NumberFormatException   e)
{
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多