将String类型的数字字符转换成int
java.lang.Integer.parseInt(String)
public static int parseInt(String s)
throws NumberFormatException
- Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign
'-'
('\u002D'
) to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to theparseInt(java.lang.String, int)
method. -
- Parameters:
s
- aString
containing theint
representation to be parsed- Returns:
- the integer value represented by the argument in decimal.
- Throws:
NumberFormatException
- if the string does not contain a parsable integer.
为什么不是
java.lang.Integer.valueOf(String)
看代码:
/**
* Returns an <code>Integer</code> object holding the
* value of the specified <code>String</code>. The argument is
* interpreted as representing a signed decimal integer, exactly
* as if the argument were given to the {@link
* #parseInt(java.lang.String)} method. The result is an
* <code>Integer</code> object that represents the integer value
* specified by the string.
* <p>
* In other words, this method returns an <code>Integer</code>
* object equal to the value of:
*
* <blockquote><code>
* new Integer(Integer.parseInt(s))
* </code></blockquote>
*
* @param s the string to be parsed.
* @return an <code>Integer</code> object holding the value
* represented by the string argument.
* @exception NumberFormatException if the string cannot be parsed
* as an integer.
*/
public static Integer valueOf(String s) throws NumberFormatException
{
return new Integer(parseInt(s, 10));
}
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
* indicate a negative value. The resulting integer value is returned.
* <p>
* An exception of type <code>NumberFormatException</code> is
* thrown if any of the following situations occurs:
* <ul>
* <li>The first argument is <code>null</code> or is a string of
* length zero.
* <li>The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* <code>'-'</code> (<code>'\u002D'</code>) provided that the
* string is longer than length 1.
* <li>The value represented by the string is not a value of type
* <code>int</code>.
* </ul><p>
* Examples:
* <blockquote><pre>
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
* </pre></blockquote>
*
* @param s the <code>String</code> containing the integer
* representation to be parsed
* @param radix the radix to be used while parsing <code>s</code>.
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the <code>String</code>
* does not contain a parsable <code>int</code>.
*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
} int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit; if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
} /**
* Parses the string argument as a signed decimal integer. The
* characters in the string must all be decimal digits, except that
* the first character may be an ASCII minus sign <code>'-'</code>
* (<code>'\u002D'</code>) to indicate a negative value. The resulting
* integer value is returned, exactly as if the argument and the radix
* 10 were given as arguments to the
* {@link #parseInt(java.lang.String, int)} method.
*
* @param s a <code>String</code> containing the <code>int</code>
* representation to be parsed
* @return the integer value represented by the argument in decimal.
* @exception NumberFormatException if the string does not contain a
* parsable integer.
*/
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
将String类型的数字字符转换成int的更多相关文章
- Swift - 将String类型的数字转换成数字类型(支持十进制、十六进制)
1,十进制的字符串转成数字 Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = ...
- Swift - 将String类型的数字转换成数字类型
Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = (tf1.text as N ...
- java如何将char类型的数字转换成int型的数字,而不是Ascii
如何把 char ‘3’ 转为 int 3, 大家应该知道,不能直接转化,那样得到是‘3’的Ascii. 如下面: public class Leet { public static void mai ...
- SQL SERVER 字符串类型varchar格式转换成int类型进行排序
日常数据分析过程中,经常会遇到排序的情况,有时会根据空字段表进行临时排序,转换数据类型 使用 ORDER BY CAST (<字段名> AS INT) ASC 举例: SELECT I ...
- 把int类型值转换成int数组(不通过string类型转换)
只适合初学者 今天同事问了我不通过string类型把int类型值123589转换成int[]数组.我想了想于是写了出来,其实不难.看你小学数学学得好不好.言归正传. 先不说代码,举个列子就知道怎么玩了 ...
- int类型的整数转换成汉字
int类型的整数转换成汉字 一.源代码:IntegerNumberToChinese.java package cn.com.zfc.example; import java.util.Scanner ...
- C# 中怎么将string转换成int型
int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...
- MySQL类型转换 使用CAST将varchar转换成int类型排序
--使用CAST将varchar转换成int类型排序 select distinct(zone_id) from guild_rank_info order by CAST(zone_id as SI ...
- Char类型与Sting类型的数字字符转换时的不同点
这是在一次编程时的bug里偶然发现的一个问题.在C#中,单引号默认是char类型字符,而双引号默认是string类型字符.对于char类型的数字字符,通过强制类型转换或者convert转换,转换成的整 ...
随机推荐
- jQuery 源码分析和使用心得 - 关于源码
说到jQuery, 大家可能直觉的认为jQuery的源码应该就是一个jquery.xx.js这样的一个文件. 但是看到真正的源码的时候, 整个人都思密达了.jQuery的源码做的事远比你想象的多, 为 ...
- inux网卡与MAC地址绑定方法总结
使用linux系统时会出现这样的情况,当你安装了某个网卡的驱动程序时,或者安装了与网卡相关的程序后. 网卡会出现所谓的漂移现象.(注意:不是飘逸).可能的表象为: (1):网卡顺序颠倒,比如之 ...
- Tomcat Remote Debug操作和原理
操作篇 这部分主要讲,如何开启tomcat远程调试,并佐以实例.本文方式适用于windows和linux. 假设有两台机器,A是tomcat服务器所在机器,B是IDE安装机器.A和B可以是同一台机器, ...
- jQuery学习-事件之绑定事件(三)
在上一篇<jQuery学习-事件之绑定事件(二)>我们了解了jQuery的dispatch方法,今天我们来学习下handlers 方法: handlers: function( event ...
- Sass使用教程
sass官网: http://sass-lang.com/ http://sass-lang.com/documentation/file.SASS_REFERENCE.html Sass和Scss的 ...
- 在 Windows Azure 网站中进行纵向扩展和横向扩展
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Byron Tardif 撰写. 当您开始一个新的 Web 项目,或者刚刚开始开发一般的网站和应用程序时,您可能希望从小处着手. ...
- 前端js模板库 JinkoTemplate
有时候需要使用ajax来异步生成html,最土的方法就是用js的‘+’连接html代码,生成繁琐.一旦需要修改,对于少量的html代码到没啥问题,要是比较复杂的样式时,就真坑爹了,眼花缭乱有木有?Ji ...
- uvalive 6657 GCD XOR
//感觉太长时间没做题 好多基本的能力都丧失了(>_<) 首先大概是这样的,因为gcd(a,b)=c,所以a,b都是c的倍数,所以我们依次枚举a的值为2c 3c 4c......,a xo ...
- #include <stdint.h>
stdint.h是c99中引进的一个标准C库的头文件. #include<stdio.h> #include<stdint.h> main() { /* 数据类型可以跨平台移植 ...
- HipHop算法:利用微博互动关系挖掘社交圈
/* 版权声明:可以任意转载,转载时请务必标明文章原始出处和作者信息 .*/ CopyMiddle: 张俊林 TimeStamp:2012年3 月 在微博环境下,如何 ...