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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • gis可视化的方法_gis可视化分析

    gis可视化的方法_gis可视化分析前言从去年开始无脑接触Cesium三维GIS可视化,入坑之后一直到到现在,其实已经写了多个项目了,中间也遇到了很多坑点,很早就想分享其中所获了,只是觉得不太专业而且没有太多时间,…

    2022年9月1日
    3
  • xshell连接不上虚拟机的问题和解决办法_vmware远程连接服务器虚拟机

    xshell连接不上虚拟机的问题和解决办法_vmware远程连接服务器虚拟机首先按照正常步骤安装虚拟机,centos6.5文件,然后登陆Linux输入命令:vi/etc/sysconfig/network-scripts/ifcfg-eth0键入i进行编辑大致如下DEVICE=eth0TYPE=EthernetONBOOT=yesBOOTPROTO=dhcpIPADDR=192.168.175.102NETMASK=255.255.255…

    2022年9月22日
    2
  • Shiro面试题(二十道)[通俗易懂]

    Shiro面试题(二十道)[通俗易懂]SecurityManager才是实际的执行者。Realm:域,shiro从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;Shiro提供了CredentialsMatcher的散列实现HashedCredentialsMatcher,和PasswordMatcher不同的是,它只是用于密码验证,且可以提供自己的盐,而不是随机生成盐,且生成密码散列值的算法需要自己写,因为能提供自己的盐。..

    2022年10月14日
    4
  • php目录分隔符DIRECTORY_SEPARATOR

    php目录分隔符DIRECTORY_SEPARATOR

    2021年11月4日
    50
  • git如何查看分支是哪个分支创建的_哪里查看QQ建立时间

    git如何查看分支是哪个分支创建的_哪里查看QQ建立时间实际应用中,可能需要准确知道指定分支的创建时间。代码实例如下:gitreflogshow–date=isomastergitreflogshow–date=iso#######[Shell]纯文本查看复制代码 1 $gitreflogshow–date=isomaster 代码运行效果截图如下:…

    2022年9月26日
    2
  • kafka是什么?主要用在什么场景

    kafka是什么?主要用在什么场景1、kafka是什么?Kafka是由LinkedIn开发的一个分布式基于发布/订阅的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用。2、产生背景Kafka是一个消息系统,用作LinkedIn的活动流(ActivityStream)和运营数据处理管道(Pipeline)的基础。活动流数据是几乎所有站点在对其网站使用情况做报表时都要用到的数据中最常规的部分。活动数据包括页…

    2022年10月13日
    3

发表回复

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

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