int32.parse什么意思_integer.parseint和valueof

int32.parse什么意思_integer.parseint和valueofnt32.Parse(string)Int32.Parse(stringstr)methodconvertsthestringrepresentationofanumbertoits32-bitsignedintegerequivalent.Ittakesastringandtriestoextractanintegerfromi

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/192712.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号