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。

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

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

(0)
上一篇 2025年12月9日 下午6:43
下一篇 2025年12月9日 下午7:15


相关推荐

  • 视频演示 | 功能新增 | 增强版在线LEFSe分析和可视化鉴定标志性基因或物种

    视频演示 | 功能新增 | 增强版在线LEFSe分析和可视化鉴定标志性基因或物种LEfSe分析即LDAEffectSize分析,是一种用于发现和解释高维度数据生物标识(基因、通路和分类单元等)的分析工具,可以进行两个或多个分组的比较,它强调统计意义和生物相关性,…

    2022年5月31日
    41
  • window.location.href的用法

    window.location.href的用法javascript中的location.href有很多种用法,主要如下。self.location.href="/url"当前页面打开URL页面location.href=&

    2022年7月4日
    27
  • Mac配置PHP环境(brew安装nginx+php)「建议收藏」

    第一步:检查本地有没有PHP:输入php-v命令,查看,如果有PHP版本,说明本地有PHP(MacOSX10.9以后的系统自带了PHP、php-fpm)我检查了一下,本地有,就不用安装了第二步:安装nginx直接输入brewinstallnginx然后回车就行,等待好一会儿,自动安装看到下面的提示,如图,说明安装成功,执行brewservicesstar…

    2022年4月16日
    36
  • pycharm新建python文件自带文件名和创建时间等

    pycharm新建python文件自带文件名和创建时间等pycharm 新建 python 文件自带文件名和创建时间等

    2026年3月19日
    2
  • aliddns ipv6_linux系统下配置阿里DDNS(IPv6)

    aliddns ipv6_linux系统下配置阿里DDNS(IPv6)IPv6日渐完善,家里的宽带和手机也都分配了ipv6全球单播地址,手机分到了/64,宽带更是分到了/56。测试了一下运营商内外的连通性也都还可以,基本能跑满带宽,IPv6终于可以用起来了,个个都是公网,不用再渴求ipv4和搞内网穿透了。适用此教程适用于基于linux的各种系统,ubuntu、centos、openwrt、群晖等等,本文只介绍IPv6,不涉及IPv4。代码aliddnsipv6_a…

    2022年6月12日
    113
  • js防抖和节流 区别及实现方式

    js防抖和节流 区别及实现方式概念 函数防抖 debounce 触发高频事件后 n 秒内函数只会执行一次 如果 n 秒内高频事件再次被触发 则重新计算时间 函数节流 throttle 高频事件触发 但在 n 秒内只会执行一次 所以节流会稀释函数的执行频率 函数节流 throttle 与函数防抖 debounce 都是为了限制函数的执行频次 以优化函数触发频率过高导致的响应速度跟不上触发频率 出现延迟 假死或卡顿的现

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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