String 与 int 类型之间的互相转换
2018-03-27 13:53
357 查看
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 转成字串的方法大同小异. int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
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)),也会抛
异常,但会多产生一个对象
1.问题思考:需要明确的是String是引用类型,int是基本类型,所以两者的转换并不是基本类型间的转换,这也是该问题提出的意义所在,SUN公司提供了相应的类库供编程人员直接使用。2.Integer.parseInt(str) 与 Integer.valueOf(Str).intValue() :其实查看Java源码不难发现后者的实现是基于parseInt函数来实现的,所以很有必要分析parseInt源码。3.Integer.parseInt(str) 源码分析:[java] view plain copypublic static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
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, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
<span style="color:#ff0000;">digit = Character.digit(s.charAt(i++),radix);</span>
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);
}
return negative ? result : -result;
}
加红源码如下:[java] view plain copypublic static int digit(char ch, int radix) {
return digit((int)ch, radix);
}
[html] view plain copy/* @param codePoint the character (Unicode code point) to be converted.
* @param radix the radix.
* @return the numeric value represented by the character in the
* specified radix.
* @see Character#forDigit(int, int)
* @see Character#isDigit(int)
* @since 1.5
*/
public static int digit(int codePoint, int radix) {
return CharacterData.of(codePoint).digit(codePoint, radix);
}
可以看出加红代码是将字符 => Unicode(字符统一编码) => Unicode(数字统一编码) => 数字。从上面的分析可以发现源码是取出字符串中的每个字符,然后将字符转换为数字进行拼接,但是在拼接的过程中SUN公司的编程人员是将其先拼接为负数,再用三元运算转换选择输出。自己并不认同,因为这样的做法是不利于理解,当然作者可能有自己的考虑,值得揣摩。4.自己动手,丰衣足食: 思路:化整为零 -> 将引用类型的String分解为char;逐个击破 -> 进本数据类型之间的转换Character.digit(ch,radix) / Character.getNumericValue(ch) 原理相同; 由点及线-> 将数字放到不同权值得相应位置上,组成int型数值。 注: 正负号判断,数值长度判断,数字合法性校验(0-9)... CODEING:[java] view plain copypublic static int change(String s){
int result = 0;<span style="white-space:pre;"> </span> //数值
int len = s.length();<span style="white-space:pre;"> </span>
int indexEnd = len - 1; //控制由右及左取字符(数字)
int indexBegin = 0;<span style="white-space:pre;"> </span> //起始位置(存在+ - 号)
boolean negative = false;<span style="white-space:pre;"> </span> //确定起始位置及输出结果标志符
int position = 1; //权值:起始位置为个位
int digit = 0;<span style="white-space:pre;"> </span> //数字
if(len > 0){
char firstChar = s.charAt(0);
if (firstChar < '0') {
if (firstChar == '-') {
negative = true;
indexBegin = 1;
}else if(firstChar == '+'){
indexBegin = 1;
}else if (firstChar != '+'){
throw new NumberFormatException(s);
}
if (len == 1) throw new NumberFormatException(s);
}
while (indexEnd >= indexBegin) {
//(int) s.charAt(indexEnd--);只是该字符的数字编码,十进制数字的Unicode码范围为48-57
if(s.charAt(indexEnd) < 48 || s.charAt(indexEnd)> 57){
throw new NumberFormatException(s);
}
//int temp = Character.getNumericValue(s.charAt(indexEnd--));
int temp = Character.digit(s.charAt(indexEnd--), 10);
digit = temp * position;
result += digit;
position *= 10;
}
}
return negative ? -result : result;
}
5.学无止境,Java升级版:[java] view plain copypublic static int myAtoi(String str) {
int index = 0, sign = 1, total = 0;
//1. Empty string
if(str.length() == 0) return 0;
//2. Remove Spaces
while(str.charAt(index) == ' ' && index < str.length()) // str = str.trim();
index ++;
//3. Handle signs
if(str.charAt(index) == '+' || str.charAt(index) == '-'){
sign = str.charAt(index) == '+' ? 1 : -1;
index ++;
}
//4. Convert number and avoid overflow
while(index < str.length()){
int digit = str.charAt(index) - '0';
if(digit < 0 || digit > 9) break;
//check if total will be overflow after 10 times and add digit
if(Integer.MAX_VALUE/10 < total || Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
total = 10 * total + digit;
index ++;
}
return total * sign;
}
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
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)),也会抛
异常,但会多产生一个对象
1.问题思考:需要明确的是String是引用类型,int是基本类型,所以两者的转换并不是基本类型间的转换,这也是该问题提出的意义所在,SUN公司提供了相应的类库供编程人员直接使用。2.Integer.parseInt(str) 与 Integer.valueOf(Str).intValue() :其实查看Java源码不难发现后者的实现是基于parseInt函数来实现的,所以很有必要分析parseInt源码。3.Integer.parseInt(str) 源码分析:[java] view plain copypublic static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
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, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
<span style="color:#ff0000;">digit = Character.digit(s.charAt(i++),radix);</span>
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);
}
return negative ? result : -result;
}
加红源码如下:[java] view plain copypublic static int digit(char ch, int radix) {
return digit((int)ch, radix);
}
[html] view plain copy/* @param codePoint the character (Unicode code point) to be converted.
* @param radix the radix.
* @return the numeric value represented by the character in the
* specified radix.
* @see Character#forDigit(int, int)
* @see Character#isDigit(int)
* @since 1.5
*/
public static int digit(int codePoint, int radix) {
return CharacterData.of(codePoint).digit(codePoint, radix);
}
可以看出加红代码是将字符 => Unicode(字符统一编码) => Unicode(数字统一编码) => 数字。从上面的分析可以发现源码是取出字符串中的每个字符,然后将字符转换为数字进行拼接,但是在拼接的过程中SUN公司的编程人员是将其先拼接为负数,再用三元运算转换选择输出。自己并不认同,因为这样的做法是不利于理解,当然作者可能有自己的考虑,值得揣摩。4.自己动手,丰衣足食: 思路:化整为零 -> 将引用类型的String分解为char;逐个击破 -> 进本数据类型之间的转换Character.digit(ch,radix) / Character.getNumericValue(ch) 原理相同; 由点及线-> 将数字放到不同权值得相应位置上,组成int型数值。 注: 正负号判断,数值长度判断,数字合法性校验(0-9)... CODEING:[java] view plain copypublic static int change(String s){
int result = 0;<span style="white-space:pre;"> </span> //数值
int len = s.length();<span style="white-space:pre;"> </span>
int indexEnd = len - 1; //控制由右及左取字符(数字)
int indexBegin = 0;<span style="white-space:pre;"> </span> //起始位置(存在+ - 号)
boolean negative = false;<span style="white-space:pre;"> </span> //确定起始位置及输出结果标志符
int position = 1; //权值:起始位置为个位
int digit = 0;<span style="white-space:pre;"> </span> //数字
if(len > 0){
char firstChar = s.charAt(0);
if (firstChar < '0') {
if (firstChar == '-') {
negative = true;
indexBegin = 1;
}else if(firstChar == '+'){
indexBegin = 1;
}else if (firstChar != '+'){
throw new NumberFormatException(s);
}
if (len == 1) throw new NumberFormatException(s);
}
while (indexEnd >= indexBegin) {
//(int) s.charAt(indexEnd--);只是该字符的数字编码,十进制数字的Unicode码范围为48-57
if(s.charAt(indexEnd) < 48 || s.charAt(indexEnd)> 57){
throw new NumberFormatException(s);
}
//int temp = Character.getNumericValue(s.charAt(indexEnd--));
int temp = Character.digit(s.charAt(indexEnd--), 10);
digit = temp * position;
result += digit;
position *= 10;
}
}
return negative ? -result : result;
}
5.学无止境,Java升级版:[java] view plain copypublic static int myAtoi(String str) {
int index = 0, sign = 1, total = 0;
//1. Empty string
if(str.length() == 0) return 0;
//2. Remove Spaces
while(str.charAt(index) == ' ' && index < str.length()) // str = str.trim();
index ++;
//3. Handle signs
if(str.charAt(index) == '+' || str.charAt(index) == '-'){
sign = str.charAt(index) == '+' ? 1 : -1;
index ++;
}
//4. Convert number and avoid overflow
while(index < str.length()){
int digit = str.charAt(index) - '0';
if(digit < 0 || digit > 9) break;
//check if total will be overflow after 10 times and add digit
if(Integer.MAX_VALUE/10 < total || Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
total = 10 * total + digit;
index ++;
}
return total * sign;
}
相关文章推荐
- java int,string,char三种类型变量之间的互相转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- java中String和int类型互相转换
- java int和String类型之间的相互转换
- java中关于String类型和double,int类型之间的转换
- 计算两个日期之间相差几天(Date类型与String类型互相转换)
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- 数据类型之间的转换(eg:String—Integer -int)
- Go语言string,int,int64 ,float之间类型转换方法
- java中int和string类型之间转换的几种方法
- String与int float double 类型之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- C#中enum,string,int 之间的互相转换
- string与int和char之间的类型转换问题
- enum枚举与int string类型之间的相互转换
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
- MySQL中Int类型、short和byte之间强制转换、String的replaceALL
- string数组类型转换为int数组(数组类型之间的转换)