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

2024-06-22 02:58

本文主要是介绍Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

Int32. 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 throw ArgumentNullException. 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.  

        Int32.Parse(string str)方法将字符串转化为32bit等值整数。它接收字符串参数,尝试从中抽取整数,并返回整数。遇到null引用时,抛出ArgumentNullException;如果字符串不是整数值,抛出FormatException;当字符串代表数字小于MinValue(−2,147,483,648) 或大于MaxValue(+2,147,483,647),抛出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.

        Convert.ToInt32(string str) 方法转换特定字符串到32bit等值整数。Convert.ToInt32其实内部调用Int32.Parse。唯一不同的是如果参数是null引用返回0,而Int32.Parse抛出ArgumentNullException。如果str不是整数值,抛出FormatException。当字符串代表数字小于MinValue(−2,147,483,648) 或大于MaxValue(+2,147,483,647),抛出OverflowException。

For example: 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 
int resultValue; 
resultValue = Convert.ToInt32(str1); //-- 123 
resultValue = Convert.ToInt32(str2); //-- FormatException 
resultValue = Convert.ToInt32(str3); //-- OverflowException 
resultValue = Convert.ToInt32(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 OverflowException as it was coming in above two methods. 

        这个方法在C#2.0及以上版本中可用。它将指定的字符串转化为out变量,如果成功转换则返回true。当参数是null引用时,返回0,而不是像前两个方法一样抛出ArgumentNullException 。如果参数不是整数,out 变量将是0,而不是抛出FormatException 。当字符串代表数字小于MinValue(−2,147,483,648) 或大于MaxValue(+2,147,483,647),out变量将是0,而不是抛出OverflowException。

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.

        所以,从上可以看出,你慢慢了解了几种方法从字符串中抽取整数。因此你应该使用最适合你需求的方法。如果你有字符串,如果期待总是返回整数,则使用Int32.Parse;如果期待除了整数还返回其他值,则用Convert.ToInt32。如果不想碰到异常,就使用Int32.TryParse。

这篇关于Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1083093

相关文章

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

分辨率三兄弟LPI、DPI 和 PPI有什么区别? 搞清分辨率的那些事儿

《分辨率三兄弟LPI、DPI和PPI有什么区别?搞清分辨率的那些事儿》分辨率这个东西,真的是让人又爱又恨,为了搞清楚它,我可是翻阅了不少资料,最后发现“小7的背包”的解释最让我茅塞顿开,于是,我... 在谈到分辨率时,我们经常会遇到三个相似的缩写:PPI、DPI 和 LPI。虽然它们看起来差不多,但实际应用

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使