[英]int.Parse Fails On Valid String
I am completely lost with this.我完全迷失了这个。
So, I'm trying to use int.TryParse
to parse a string, a perfectly valid string containing literally the number "3", that's it.所以,我试图使用
int.TryParse
来解析一个字符串,一个完全有效的字符串,字面上包含数字“3”,就是这样。 But, it's giving me back false when I try to parse it.但是,当我尝试解析它时,它让我返回 false。
This is how I'm using it - I've debugged it, and int.TryParse
is definitely giving back false, as the code in the if statement runs:这就是我使用它的方式 - 我已经调试了它,并且
int.TryParse
肯定会返回 false,因为 if 语句中的代码运行:
if (!int.TryParse(numberSplitParts[0], out int hour))
return false;
And I've looked in the debugger numberSplitParts[0]
is definitely the digit 3, that's perfectly valid!而且我查看了调试器
numberSplitParts[0]
绝对是数字 3,这是完全有效的!
Now, I did some research and people have been saying to use the invariant CultureInfo, so, I did that, here's the new updated line (I also tried NumberStyles.Any
and that didn't work either):现在,我做了一些研究,人们一直在说要使用不变的 CultureInfo,所以,我这样做了,这是新的更新行(我也尝试过
NumberStyles.Any
,但也没有用):
if (!int.TryParse(numberSplitParts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out int hour))
return false;
That also doesn't work - it continues to give me back false
, and hour
is 0
.这也不起作用 - 它继续给我返回
false
,而hour
是0
。
I've tried all of the other number types as well - byte.Parse
, Int16.Parse
etc. All of those gave back false
too.我也尝试了所有其他数字类型 -
byte.Parse
、 Int16.Parse
等。所有这些也都返回false
。
And, I've tried regular int.Parse
, and that simply gives me the following exception:而且,我已经尝试过常规的
int.Parse
,这只是给了我以下异常:
System.FormatException : 'Input string was not in a correct format.'
System.FormatException : '输入字符串的格式不正确。'
But then, I tried it in a different project, so, I replicated the string array and everything, and it worked there - both with and without "InvariantCulture".但是后来,我在一个不同的项目中尝试了它,因此,我复制了字符串数组和所有内容,并且它在那里工作- 无论有没有“InvariantCulture”。
So, I'm suspecting that the project I'm working in must be configured in such a way that caused int.Parse
/ int.TryParse
to not work.因此,我怀疑我正在处理的项目必须以导致
int.Parse
/ int.TryParse
无法工作的方式进行配置。 This is in a class library, that is being accessed from a UWP Application - could the fact that this is running under UWP have any effect?这是在从UWP 应用程序访问的类库中 - 在 UWP 下运行的事实是否有任何影响?
As discussed in the comments, this is due to a couple of LEFT-TO-RIGHT MARK Unicode characters in your input.正如评论中所讨论的,这是由于您的输入中有几个LEFT-TO-RIGHT MARK Unicode 字符造成的。
When you did your test in a different project, you probably hard-coded the string "3"
, or got your input from a source which didn't add the same invisible characters.当您在不同的项目中进行测试时,您可能对字符串
"3"
了硬编码,或者从没有添加相同不可见字符的源中获取输入。
A better test is to check whether numberSplitParts[0] == "3"
, either in the watch window or in your code itself.更好的测试是检查
numberSplitParts[0] == "3"
是否在监视窗口中或在您的代码本身中。 Another is to set numberSplitParts[0] = "3"
in your UWP project, and see if that parses correctly.另一种方法是在您的 UWP 项目中设置
numberSplitParts[0] = "3"
,然后查看是否正确解析。
In my experience, most cases of "this string looks fine but <stuff> fails" is due to invisible Unicode characters sneaking into your input.根据我的经验,“这个字符串看起来不错但 <stuff> 失败”的大多数情况是由于不可见的 Unicode 字符潜入您的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.