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

相关文章

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

Python中@classmethod和@staticmethod的区别

《Python中@classmethod和@staticmethod的区别》本文主要介绍了Python中@classmethod和@staticmethod的区别,文中通过示例代码介绍的非常详细,对大... 目录1.@classmethod2.@staticmethod3.例子1.@classmethod

Golan中 new() 、 make() 和简短声明符的区别和使用

《Golan中new()、make()和简短声明符的区别和使用》Go语言中的new()、make()和简短声明符的区别和使用,new()用于分配内存并返回指针,make()用于初始化切片、映射... 详细介绍golang的new() 、 make() 和简短声明符的区别和使用。文章目录 `new()`

Python中json文件和jsonl文件的区别小结

《Python中json文件和jsonl文件的区别小结》本文主要介绍了JSON和JSONL两种文件格式的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下... 众所周知,jsON 文件是使用php JSON(JavaScripythonpt Object No

结构体和联合体的区别及说明

《结构体和联合体的区别及说明》文章主要介绍了C语言中的结构体和联合体,结构体是一种自定义的复合数据类型,可以包含多个成员,每个成员可以是不同的数据类型,联合体是一种特殊的数据结构,可以在内存中共享同一... 目录结构体和联合体的区别1. 结构体(Struct)2. 联合体(Union)3. 联合体与结构体的

什么是 Ubuntu LTS?Ubuntu LTS和普通版本区别对比

《什么是UbuntuLTS?UbuntuLTS和普通版本区别对比》UbuntuLTS是Ubuntu操作系统的一个特殊版本,旨在提供更长时间的支持和稳定性,与常规的Ubuntu版本相比,LTS版... 如果你正打算安装 Ubuntu 系统,可能会被「LTS 版本」和「普通版本」给搞得一头雾水吧?尤其是对于刚入