惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
爱范儿
爱范儿
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
B
Blog RSS Feed
博客园 - 聂微东
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Scott Helme
Scott Helme
AI
AI
S
Security Affairs
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
AWS News Blog
AWS News Blog
T
Threatpost
Cyberwarzone
Cyberwarzone
www.infosecurity-magazine.com
www.infosecurity-magazine.com
U
Unit 42
V
Vulnerabilities – Threatpost
J
Java Code Geeks
博客园 - Franky
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
NISL@THU
NISL@THU
D
Docker
小众软件
小众软件
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
Arctic Wolf
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
量子位
PCI Perspectives
PCI Perspectives
美团技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
有赞技术团队
有赞技术团队
腾讯CDC
P
Proofpoint News Feed
S
Security @ Cisco Blogs
G
Google Developers Blog
C
Cisco Blogs

博客园 - Adrian wang

簇表BSEG在联合查询时用什么表去替代 [推荐] 对日软件开发文档式样书写常用日语精粹 几个常用的T-Code 001 SAP R/3财务基本概念及集成性浅释---总帐等概念 SAP R/3 财务基本概念及集成性浅释 —— 主数据篇 SAP技术人员必会技能(转帖) 使用 Microsoft .NET 的企业解决方案模式 T-SQL 编码标准(择自MSDN) 数据库表中的空值(NULL) 总结:ADO.NET在开发中的部分使用方法和技巧 (转贴) 编译器错误信息 CS1595 学日语、记单词是有规律的 不用鼠标换了日语输入法 外语学习忌 ! RegisterStartupScript和RegisterClientScriptBlock的用法 数据库开发个人总结(ADO.NET) 使用ADO.NET轻松操纵数据库 关于何种情况下使用DataGrid、DataList或Repeater的一些讨论 彻底删除项目的VSS源代码管理信息
如何把string解析为int?
Adrian wang · 2006-03-01 · via 博客园 - Adrian wang

Q:如何把string解析为int?

A:简单的方法有三种:

string source = "1412";
int result = 0;

// 使用Convert.ToInt32(string value);
result = Convert.ToInt32(source);

// 使用Int32.Parse(string value);
result = Int32.Parse(source);

// 使用Int32.TryParse(string s, out int result);
Int32.TryParse(source, out result);

Q:这三种方法有什么不同?

A:一个简单的回答是:

如果解析失败,Int32.Parse(source)总会抛出异常;Convert.ToInt32(source)在source为null的情况下不会抛出异常而是简单的返回0给调用方;而Int32.TryParse(source, result)则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值。

Q:如果我要解析的字符串的字面数值不是十进制的话,那么从这些方法中得到的返回值是有问题的。有什么方法解决?

A:那么你就需要这些方法的对应重载版本了,一个简单的方法是使用Convert类的

public static int ToInt32(string value, int fromBase);

其中fromBase的值只能为2、8、10或者16,用于指定进制方式。如果fromBase不是指定的数值或者value不为十进制而又带有前缀正负号,就会抛出ArgumentException。

string source = "0x1412"; // 这里的0x(或0X)前缀是可选的。
int result = Convert.ToInt32(source, 16);

当然,你还可以通过为Int32类的

public static int Parse(string s, NumberStyles style);

指定NumberStyles.AllowHexSpecifier或者NumberStyles.HexNumber为第二个参数来解析十六进制字面值的字符串,此时,你需要引用System.Globalization命名空间。

或者使用Int32类的

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result);

并指定NumberStyles.AllowHexSpecifier或者NumberStyles.HexNumber为第二个参数,null为第三个参数来解析十六进制字面值的字符串。你当然也应该引用System.Globalization命名空间。

这里有一点要提醒的是,无论使用Parse或者TryParse方法来解析十六进制,字符串都不能出现0x或0X前缀,否则将会抛出异常。

Q:如果我要把使用科学记数法表示的string转换为int又该如何呢?

A:你可以通过把NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent(把两个NunberStyles枚举进行位运算,其中前者说明可能存在小数点,而后者则说明可能存在科学记数法的指数符号)作为第二个参数传递给Int32类的

public static int Parse(string s, NumberStyles style);

或者

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result);

如果解析出来的结果与int不兼容的,就要考虑把结果储存在别的类型了。例如"1.412e2"就应该把解析结果存放到float或者double或者decimal类型的变量里,当然,你也应该使用与储存变量相对应的类型的方法来解析:

double result = Double.Parse("1.412e2", NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);

整个字符串表达式应该没有任何任何空格,否则将有可能抛出异常。

科学记数法的格式为[{+|-}]m.dddddd{e|E}[{+|-}]xx,可以分解为以下几种形式:

[-]m.dddddde+xx 
[-]m.dddddde-xx 
[-]m.ddddddE+xx 
[-]m.ddddddE-xx
下面列举一些不能正常解析的:

"1.412 e3" 
"1.412E 3" 
"1.412e +3" 
"141200E- 2"
Q:如何处理待解析string里所包含的空格?

A:对于字符串的前缀或后缀空格,你同样有多种选择,一般情况下,你可以直接使用String类的

public string Trim();

来取掉头尾可能包含的空格:

int result = Int32.Parse(textBox1.Text.Trim());

又或者,你使用NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite来告诉Parse或TryParse待解析字符串的头尾可能包含着空格。

int result = Int32.Parse(textBox1.Text, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);

如果待解析的字符串使用科学记数法来表示,那么你可以

int result = Int32.Parse(" 1.412e3 ", NumberStyles.Float);

其中NumberStyles.Float告诉Parse方法待解析的字符串可能前缀或后缀的空格、前缀正负号、(十进制)小数点、科学记数法指数表示等。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=342178