可能重复:
如何将字符串转换为整数?
你好,
我遇到了将字符串转换为整数的问题:
string str = line.Substring(0,1);
//This picks an integer at offset 0 from string 'line'
现在字符串 str 中只包含一个整数。我正在执行以下操作:
int i = Convert.ToInt32(str);
如果我按照以下语句写,应该会打印出一个整数,对吗?
Console.WriteLine(i);
代码编译无误,但在运行时却出现以下错误:
未经处理的异常类型“System.FormatException”发生于 mscorlib.dll 中。
更多信息:输入字符串格式不正确。
请问有什么帮助吗?