Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别

简介:

nt32. Parse (string)


Int32.Parse (string str) method converts the string representation of a number to its 32-bit signed integer equivalent. It takes a string and tries to extract an integer from it and returns the integer. When s is a null reference, it will throwArgumentNullException. If str is not an integer value, it will throw FormatException. When str represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example:

 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 

resultValue = Int32.Parse(str1); //-- 123 
resultValue = Int32.Parse(str2); //-- FormatException 
resultValue = Int32.Parse(str3); //-- OverflowException 
resultValue = Int32.Parse(str4); //-- ArgumentNullException 



Convert.ToInt32(string)


Convert.ToInt32(string str) method converts the specified string representation of 32-bit signed integer equivalent. Convert.ToInt32 underneath calls the Int32.Parse. The only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. If str is other than integer value, it will throw FormatException. When s represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example: 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 


resultValue = Int32.Parse(str1); //-- 123 
resultValue = Int32.Parse(str2); //-- FormatException 
resultValue = Int32.Parse(str3); //-- OverflowException 
resultValue = Int32.Parse(str4); //-- 0 


Int32.TryParse(string, out int)


This method is available in C# 2.0 and above. Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully else false. When input string   is a null reference, it will return 0 rather than throw ArgumentNullException as it was coming in above two methods. If input string  is other than an integer value, the out variable will have 0 rather than FormatException as it was coming in above two methods. When input string  represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowExceptionas it was coming in above two methods. For example

 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 
bool isParsed;
isParsed =Int32.TryParse(str1, out resultValue); //isParsed =>true; result => 123

isParsed =Int32.TryParse(str2, out resultValue); //isParsed => false; result => 0 
isParsed =Int32.TryParse(str3, out resultValue); //isParsed => false; result => 0 
isParsed =Int32.TryParse(str4, out resultValue); // isParsed => false; result => 0 


So from above you came to know about s several different ways to extract integers from strings in
.Net. You should therefore use the method that better suits your scenario. If you've got a string, and you expect it to always be an integer use Int32.Parse.If you are expecting input other than integer use Convert.ToInt32 and if you don’t want any exception you can go for Int32.TryParse


目录
相关文章
|
3月前
|
Java
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
这篇文章解释了Java中`int`基本数据类型和其包装类`Integer`之间的区别,并指出在进行`==`运算时,`Integer`会拆箱为`int`类型,然后比较它们的值是否相等。
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
|
6月前
|
缓存 安全 Java
Java的Integer和int有什么区别?
Java的Integer和int有什么区别?
60 1
|
6月前
|
存储 编译器 程序员
int 和 long 的区别
int 和 long 的区别
|
存储 Java 编译器
|
6月前
|
存储 C语言
学习总结(位操作符;循环输入的三种方式;交换两个变量值的三种方法;打印数字对应的二进制;unsigned int 与int 的区别;改变特定位数0/1;&&和||的连续操作(与前置,后置结合))
学习总结(位操作符;循环输入的三种方式;交换两个变量值的三种方法;打印数字对应的二进制;unsigned int 与int 的区别;改变特定位数0/1;&&和||的连续操作(与前置,后置结合))
69 0
|
C语言
const int *和int * const的区别【C语言/指针】
const int *和int * const的区别【C语言/指针】
72 0
|
存储 缓存 Java
【Java】int和Integer的区别?为什么有包装类?
【Java】int和Integer的区别?为什么有包装类?
const int*p 与 int const *p与 int * const p与const int * const p的区别(有明显对比,超级详细,超级好记)
当只有一个const并且const在*左边时: (const int*p 与 int const *p,)*p的值不能改,但是p(地址)能改.我们可以看到第一,三张图片第五行*p下面有红线表示错误.而第二,四张没有红线的地方表示正确.所以我们可知道const int*p 与 int const *p是相同的只是书写方法不同.
68 0
C++ int const 和 const int 的区别
C++ int const 和 const int 的区别
173 0
|
存储 关系型数据库 MySQL
MySQL 中 int (10) 和 char (10) 和 varchar (10) 的区别
MySQL 中 int (10) 和 char (10) 和 varchar (10) 的区别
178 0