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


相关推荐

  • getservletcontext.getrealpath_request.getattribute取不到值

    getservletcontext.getrealpath_request.getattribute取不到值关于serveletContext.getRealPath()方法1.关于request.getRealPath问题:Stringfilename=request.getRealPath(filename)——————-信息:warning:[deprecation]getRealPath(java.

    2022年9月2日
    2
  • Centos7安装nginx1.8.0步骤

    Centos7安装nginx1.8.0步骤前言Nginx(enginex)是一个高性能的HTTP和反向代理web服务器nginx安装环境》nginx是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。gcc安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,(一)安装gcc:yuminstallgcc-c++PCREPCRE(PerlCompatibleRegularExpressions)是一个Perl库,包括perl兼容的正则表达式库。ng

    2022年6月6日
    66
  • Oracle基础–PL/SQL编程基本语法[通俗易懂]

    Oracle基础–PL/SQL编程基本语法[通俗易懂]一、概念什么是PL/SQL?1.PL/SQL(ProcedureLanguage/SQL)2.PLSQL是Oracle对sql语言的过程化扩展(类似于Basic)3.指在SQL命令语言中增加了过程处理语句(如分支、循环等),使SQL语言具有过程处理能力。二、程序结构通过plsqlDeveloper工具的TestWindow创建程序模版。1.PL/SQL可以分为三个部分:声明部分、可执行部分、异常处理部分。声明部分:此部分是以关键字DECLARE开…

    2022年10月11日
    0
  • Linux之软件安装

    Linux之软件安装3.软件安装3.1软件安装介绍学软件开发,各种台的软件熟练安装是必须要熟练掌握。大家都知道,Windows下安装软件时,只需用鼠标双击软件的安装程序,或者用Zip等解压缩软件解压缩即可安装;在android或者apple中安装软件时,只需要在手机应用商店点击安装即可。而在Linux下安装软件难度高于Windows、Android、ios和windowsphone下软件安装。下面我就详细讲…

    2022年5月28日
    30
  • sourceinsight注册码安装汉化教程_sourceinsight4.0中文乱码

    sourceinsight注册码安装汉化教程_sourceinsight4.0中文乱码注册码:SI3US-230590-09757SI3US-840598-11493SI3US-404808-04697SI3US-510811-93484SI3US-343066-11287

    2022年10月3日
    0
  • kafka 集群配置_kafka集群原理

    kafka 集群配置_kafka集群原理一、kafka简述1、简介kafka是一个高吞吐的分布式消息队列系统。特点是生产者消费者模式,先进先出(FIFO)保证顺序,自己不丢数据,默认每隔7天清理数据。消息列队常见场景:系统之间解耦合、峰值压力缓冲、异步通信。2、集群介绍(1)Kafka架构是由producer(消息生产者)、consumer(消息消费者)、borker(kafka集群的server,负责处理消息读、…

    2022年8月31日
    1

发表回复

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

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