本文介绍了int.Parse()与领先的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何prevent的code从下方投掷出现FormatException 。我希望能够分析具有前导零的字符串转换为整数。有没有干净的方式做到这一点?

 字符串值=01;
INT I = int.Parse(值);
 

解决方案

您code运行对我来说,没有一个出现FormatException (一旦你正确把握方法)

 字符串值=01;
INT I = int.Parse(值);
 

但这环的老钟;一个问题,我几年前,微软接受反的Windows(不是.NET)的本地化组件的错误。为了测试是否你看到的是,运行此code,让我们知道你是否得到一个出现FormatException:

 字符串值=0; //只是一个零
INT I = int.Parse(值);
 

修改:这里是从新闻组我的文章,从早在2007年看症状是否符合你的

...和这里的有关问题在Microsoft Connect上报告:

How do I prevent the code below from throwing a FormatException. I'd like to be able to parse strings with a leading zero into ints. Is there a clean way to do this?

string value = "01";
int i = int.Parse(value);
解决方案

Your code runs for me, without a FormatException (once you capitalize the method properly):

string value = "01";
int i = int.Parse(value);

But this does ring an old bell; a problem I had years ago, which Microsoft accepted as a bug against localization components of Windows (not .NET). To test whether you're seeing this, run this code and let us know whether you get a FormatException:

string value = "0"; // just a zero
int i = int.Parse(value);

EDIT: here's my post from Usenet, from back in 2007. See if the symptoms match yours.

...and here's a report on Microsoft Connect about the issue:

这篇关于int.Parse()与领先的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:32
查看更多