zoukankan      html  css  js  c++  java
  • 字符串、String等转换

    (1) String 转换为字符串

       例:
    String s = "abcde";
    char[] a = s.toCharArray();



    (2) 字符串转换为String
    char[] a;
    a = {a,b,c,d};
    String s;
    s = new String(a);



    (3) int类型转换为String
       例:
    int n = 0;
    String s = String.valueOf(n)



    (4)int -> String


    int i=12345;
    String s="";
    第一种方法:s=i+"";
    第二种方法:s=String.valueOf(i);
    这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?



    (5)String -> int


    s="12345";
    int i;
    第一种方法:i=Integer.parseInt(s);
    第二种方法:i=Integer.valueOf(s).intValue();

    这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
    以下是答案:


    第一种方法:s=i+"";   //会产生两个String对象
    第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象


    第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
    第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
     


     
        JAVA数据类型转换 


            这是一个例子,说的是JAVA中数据数型的转换.供大家学习

     

     

    1. public class TypeChange {  
    2.            public TypeChange() {  
    3.            }  
    4.            //change the string type to the int type  
    5.            public static   int stringToInt(String intstr)  
    6.            {  
    7.              Integer integer;  
    8.              integer = Integer.valueOf(intstr);  
    9.              return integer.intValue();  
    10.            }  
    11.            //change int type to the string type  
    12.            public static String intToString(int value)  
    13.            {  
    14.              Integer integer = new Integer(value);  
    15.              return integer.toString();  
    16.            }  
    17.            //change the string type to the float type  
    18.            public static   float stringToFloat(String floatstr)  
    19.            {  
    20.              Float floatee;  
    21.              floatee = Float.valueOf(floatstr);  
    22.              return floatee.floatValue();  
    23.            }  
    24.            //change the float type to the string type  
    25.            public static String floatToString(float value)  
    26.            {  
    27.              Float floatee = new Float(value);  
    28.              return floatee.toString();  
    29.            }  
    30.            //change the string type to the sqlDate type  
    31.            public static java.sql.Date stringToDate(String dateStr)  
    32.            {  
    33.              return   java.sql.Date.valueOf(dateStr);  
    34.            }  
    35.            //change the sqlDate type to the string type  
    36.            public static String dateToString(java.sql.Date datee)  
    37.            {  
    38.              return datee.toString();  
    39.            }  
    40.   
    41.            public static void main(String[] args)  
    42.            {  
    43.              java.sql.Date day ;  
    44.              day = TypeChange.stringToDate("2003-11-3");  
    45.              String strday = TypeChange.dateToString(day);  
    46.              System.out.println(strday);  
    47.            }  
    48.   
    49.         }  
  • 相关阅读:
    屏幕尺寸相关
    关于sqlite的数据库操作
    Service服务
    BroadcastReceiver广播接收器
    将博客搬至CSDN
    win7+WinDbg调试系统内核
    驱动
    驱动开发,走起!!哈哈
    动态链接库DLL
    2013年12月24号感受
  • 原文地址:https://www.cnblogs.com/Cherry-B/p/3338171.html
Copyright © 2011-2022 走看看