C#将字符串转换为整型
2011-10-13 14:59
218 查看
int numVal = Convert.ToInt32("29");
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.
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.
相关文章推荐
- 字符串转换为整型数字(C#)
- C#中把字符串String转换为整型Int的小例子
- c#将字符串转换为整型
- C#将字符串转换为整型的三种方法的总结
- C#将字符串转换为整型的三种方法的总结
- C#将字符串转换为整型的三种方法的总结
- C#将字符串转换为整型的三种方法的总结
- C#枚举和字符串以及整型数之间的转换
- C#将字符串转换为整型的三种方法
- c# 如何将带小数点的字符串转换为整型
- C#将字符串转换为整型的三种方法的总结
- C#将字符串转换为整型的三种方法的总结
- C#中字符串转换整型方法(三种方式的比较)
- C#中将字符串转换为整型的三种解决方法总结
- C#将字符串转换为整型的三种方法
- C#将字符串转换为整型的三种方法的总结
- C#Base64编码的字符串与图片的转换
- C语言整型和字符串之间的转换
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
- C#字符串转换比较(常用方法) 2011.3