C#将字符串转换为整型
2011-10-13 14:59 Eric.Hu 阅读(473) 评论(0) 编辑 收藏 举报numVal++;
Console.WriteLine(numVal);
// Output: 30
=================================================
int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// Output: -105
==================================================
int j;
Int32.TryParse("-105", out j);
Console.WriteLine(j);
// Output: -105
==================================================
try
{
int m = Int32.Parse("abc");
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.
===================================================
string inputString = "abc";
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);
if (!parsed)
Console.WriteLine("Int32.TryParse could not parse '{0}' to an int.\n", inputString);
// Output: Int32.TryParse could not parse 'abc' to an int.