相关疑难解决方法(0)

将String转换为Integer

可能重复:
如何将String转换为Int?

嗨,

我有以下问题将字符串转换为整数:

string str = line.Substring(0,1);

//This picks an integer at offset 0 from string 'line' 
Run Code Online (Sandbox Code Playgroud)

所以现在string str包含一个整数.我正在做以下事情:

int i = Convert.ToInt32(str);
Run Code Online (Sandbox Code Playgroud)

如果我写下面的语句,我应该打印一个整数吗?

Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud)

它编译时没有任何错误,但在运行时出现以下错误:

mscorlib.dll中发生了未处理的"System.FormatException"类型异常

附加信息:输入字符串的格式不正确.

有什么帮助吗?

c# string

9
推荐指数
1
解决办法
6万
查看次数

标签 统计

c# ×1

string ×1