字符串数组到整数数组的转换 [英] String Array to Int Array Convertion

查看:97
本文介绍了字符串数组到整数数组的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试使用以下代码将字符串数组转换为int数组:

Hello,

I am trying to convert a string array to int array using the following code:

stringArray[i, 0] = (Reader["Test"].ToString());
stringArray[i, 1] = (Reader["Test1"].ToString());
stringArray[i, 2] = (Reader["Test2"].ToString());

int toInt0 = Int32.Parse(stringArray[i, 0]);
int toInt1 = Int32.Parse(stringArray[i, 1]);
int toInt2 = Int32.Parse(stringArray[i, 2]);

intArray[i, 0] = toInt0;
intArray[i, 1] = toInt1;
intArray[i, 2] = toInt2;



我收到以下错误:

System.FormatException:输入字符串的格式不正确.
在System.Number.StringToNumber(String str,NumberStyles选项,NumberBuffer&数字,NumberFormatInfo信息,布尔值parseDecimal)
在System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)
在System.Int32.Parse(String s)
在C:\ Users \ Dave \ Documents \ Visual Studio 2010 \ Projects \ Book \ Form1.cs:line 603中的Book.button1.test()处捕获到异常.

我该怎么解决?

谢谢,
Dave



I get the following error:

System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at Book.button1.test() in C:\Users\Dave\Documents\Visual Studio 2010\Projects\Book\Form1.cs:line 603 Exception caught.

What should i fix ?

Thanks,
Dave

推荐答案

您的代码正确.
这就是它:格式不正确的字符串.

如果Parse的参数是"-33321",它将返回相应的整数值;如果它是"blah-blah",您还能期望什么?不要使用TryParse,将其保留在代码中,使用调试器,然后找到"blah-blah"在字符串数组中的位置.

如果仍然需要帮助,请问一个后续问题,但向您显示数据和编写字符串的代码,而不是转换为整数.

重要!请不要将其发布为问题(常见错误),请使用改进答案",在其他情况下,请使用添加评论".

—SA
This is expected; your code is correct.
This what it is: string in not in correct format.

If the argument of Parse is "-33321", it will return correspondent integer value, if it is "blah-blah", what else could you expect? Don''t use TryParse, keep it as it is now in your code, use debugger and find where "blah-blah" gets in your string array.

If you still need help, ask a follow-up question, but show you data and the code writing your string, not converting into integers.

Important! Please don''t post it as a Question (a common mistake), use "Improve Answer", in other cases, use "Add comment".

—SA


这篇关于字符串数组到整数数组的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆