将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转换,转换成的整 ...
随机推荐
- mysql学习(十一)嵌套查询 排序 分组
select * from products where id in(select id from cats where name like '%java%');//查找类型中名字中包含java的的商 ...
- eclipse 和myEclipse 项目导入
经常在eclipse/myeclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 有两种情况: 一.eclipse无法识别其他eclipse的web项目 解决步骤: ...
- CSS自学笔记(8):CSS拓展(一)
CSS元素对齐 可以使用margin属性类进行元素的水平对齐 水平对齐块元素时,可以将块元素的margin属性定义为"auto",这里需要注意的是,应该要声明!DOCTYPE,否则 ...
- [转]C 语言指针的使用
第一章 指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的 类型,指针的值或者叫指针所指向的内存区,还有 ...
- meteor学习
meteor学习 描述:是一套完整的用于开发现代化跨平台实时应用的整体解决方案 不是IDE(集成开发环境) 不是API接口 不是前端框架 不是后端框架 包含 命令行工具 meteor command ...
- MAC OS X 快捷键(自己总结)
command+space 可以切换键盘输入法:长按可进入输入法列表,并在多个输入法之间切换,输入法列表会根据你最近使用过的输入法自动调整排序. HID:00 00 91 00 00 00 00 00 ...
- SQL Server创建LinkServer
USE [master] GO /****** Object: LinkedServer [xxx_LNK] Script Date: 2014/7/7 17:04:13 ******/ EXEC m ...
- 数据库设计与SQL优化的建议
1. 用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分开的操作尽量分开处理,提高每次的响应速度:在数据窗口使用 ...
- MYSQL 简单的循环存储过程
BEGIN ; ; DO DO ); ; ) THEN ; END IF; END WHILE; ; ; END WHILE; END
- 剑指Offer:面试题25
题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.九度OJ地址:http://ac.jobdu.c ...