java --整形字符串转化

字符串转化为整形

Java中有两个可选的方法用来将字符串转成整型。

一个是Integer.parseInt(),另外一个是Ingeger.valueOf()。

这两个方法都是java.lang.Integer类中的静态方法。

当输入的字符串不是有效的整数,这两个方法都会抛出NumberFormatException异常。

Integer.parseInt()和Integer.valueOf()最主要的不同的就是Integer.parseint()方法返回基础数据类型int

而valueOf()返回的是java.lang.Integer对象。
Java程序,使用Integer.parseInt()方法将字符串转化为整型:

public class StringToInteger
{
public static void main(String[] args)
{
String s = "2015";
int i = Integer.parseInt(s);
System.out.println(i); //Output : 2015
}
}

注:

1如何将字串 String 转换成整数 int?

  A. 有两个方法:

  1). int i = Integer.parseInt([String]); 或

  i = Integer.parseInt([String],[int radix]);

  2). int i = Integer.valueOf(my_str).intValue();

  注: 字串转成 Double, Float, Long 的方法大同小异.

整形转化为字符串

 2 如何将整数 int 转换成字串 String ?

  A. 有叁种方法:

  1.) String s = String.valueOf(i);

  2.) String s = Integer.toString(i);

  3.) String s = "" + i;

  注: Double, Float, Long 转成字串的方法大同小异.

 1  package cn.com.lwkj.erts.reGISter;
 2 
 3   import java.sql.Date;
 4 
 5   public class TypeChange {
 6 
 7   public TypeChange() {
 8 
 9   }
10 
11   //change the string type to the int type
12 
13   public static int stringToInt(String intstr)
14 
15   {
16 
17   Integer integer;
18 
19   integer = Integer.valueOf(intstr);
20 
21   return integer.intValue();
22 
23   }
24 
25   //change int type to the string type
26 
27   public static String intToString(int value)
28 
29   {
30 
31   Integer integer = new Integer(value);
32 
33   return integer.toString();
34 
35   }
36 
37   //change the string type to the float type
38 
39   public static float stringToFloat(String floatstr)
40 
41   {
42 
43   Float floatee;
44 
45   floatee = Float.valueOf(floatstr);
46 
47   return floatee.floatValue();
48 
49   }
50 
51   //change the float type to the string type
52 
53   public static String floatToString(float value)
54 
55   {
56 
57   Float floatee = new Float(value);
58 
59   return floatee.toString();
60 
61   }
62 
63   //change the string type to the sqlDate type
64 
65   public static java.sql.Date stringToDate(String dateStr)
66 
67   {
68 
69   return java.sql.Date.valueOf(dateStr);
70 
71   }
72 
73   //change the sqlDate type to the string type
74 
75   public static String dateToString(java.sql.Date datee)
76 
77   {
78 
79   return datee.toString();
80 
81   }
82 
83   public static void main(String[] args)
84 
85   {
86 
87   java.sql.Date day ;
88 
89   day = TypeChange.stringToDate("2003-11-3");
90 
91   String strday = TypeChange.dateToString(day);
92 
93   System.out.println(strday);
94 
95   }
96 
97   }
View Code

JAVA中常用数据类型转换函数

  虽然都能在JAVA API中找到,整理一下做个备份。

 1 string->byte
 2 
 3   Byte static byte parseByte(String s)
 4 
 5   byte->string
 6 
 7   Byte static String toString(byte b)
 8 
 9   char->string
10 
11   Character static String to String (char c)
12 
13   string->Short
14 
15   Short static Short parseShort(String s)
16 
17   Short->String
18 
19   Short static String toString(Short s)
20 
21   String->Integer
22 
23   Integer static int parseInt(String s)
24 
25   Integer->String
26 
27   Integer static String tostring(int i)
28 
29   String->Long
30 
31   Long static long parseLong(String s)
32 
33   Long->String
34 
35   Long static String toString(Long i)
36 
37   String->Float
38 
39   Float static float parseFloat(String s)
40 
41   Float->String
42 
43   Float static String toString(float f)
44 
45   String->Double
46 
47   Double static double parseDouble(String s)
48 
49   Double->String
50 
51   Double static String toString(Double d)
原文地址:https://www.cnblogs.com/Catherinezhilin/p/10459681.html