string 转换成 integer 的方式有哪些?原理是什么?

回答·5
最热
最新
  • .Integer转换成int的方法 Integer i = new Integer(10);  int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i = 10; Integer it = new Integer(i); 3.String转换成int的方法 String str = "10";   Integer it = new Interger(str); int i = it.intValue(); 即:int i = Integer.intValue(string); 4.int转换成String int i = 10; (1)String s = String.valueOf(i); (2)String s = Ingeger.toString(i); (3)String s = "" + i; 5.String转换成Integer String str = "10"; Integer it = Integer.valueOf(str); 6.Integer转换成String Integer it = new Integer(10); String str = it.toString(); 7.String转换成BigDecimal BigDecimal bd = new BigDecimal(str); 8.日期 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DATE); //获取今天的日期字符串 String today = java.text.DateFormat.getDateInstance().format(new java.util.Date()); //获取今天的日期 new java.sql.Date(System.currentTimeMillis());
  • //String 转化为 Integer 时,一定要对 String 进行非空判断,否则很可能报空指针异常。 String str = "..."; Integer i = null; if(str!=null){      i = Integer.valueOf(str); }
  • int a = Integer.parseInt(str); int b = Integer.valueOf(str).intValue()
  • 先拆解成数字元素。再乘以位权相加。
  • api 文档中的方法,有解释可以看一下,原理的话上面也有一定的说明 static int parseInt(String s)  将字符串参数解析为带符号的十进制整数。   static int parseInt(String s, int radix)  将字符串参数解析为第二个参数指定的基数中的有符号整数。   static Integer valueOf(String s)  返回一个 Integer 对象,保存指定的值为 String 。   static Integer valueOf(String s, int radix)  返回一个 Integer 对象,保存从指定的 String 中 String 的值,当用第二个参数给出的基数进行解析时。