如何在C#中将某个字符串[index]转换为int?

如何在C#中将某个字符串[index]转换为int?

问题描述:

我有此代码:

string number = "124235245423523423", number2 = "3423525232332325423";
for(i=number.length-1;i>=0;i--){
int save = Convert.ToInt32(number[i]) + Convert.ToInt32(number2[i]);
}

那不是完整的代码,但是我的问题是为什么我不能转换和访问字符串的某个索引处的某些值作为整数?难道没有一个直截了当的方法吗?我已经尝试了一些方法,但是没有成功.

That is not the complete code but my question is why can't I convert and access some value at a certain index of a string as an integer? Isn't there a straight forward approach to this? I have tried some things but it didn't work out.

您正在寻找 int.Parse .

int save = int.Parse(number[3].ToString());

char 转换为 Int32 会返回当前编码中该字符的值.

Converting a char to Int32 returns the value of that character in the current encoding.

有关更多信息,请参见 Int32.Parse的MSDN文档.

For more information, see the MSDN documentation for Int32.Parse.