C#中int转成string,string转成int。

1,int转成string
用toString 
或者Convert.toString()如下 

例如:
int varInt = 1; 
string varString = Convert.ToString(varInt); 
string varString2 = varInt.ToString();

2,string转成int
如果确定字符串中是可以转成数字的字符,可以用int.Parse(string s),该语句返回的是转换得到的int值;
如果不能确定字符串是否可以转成数字,可以用int.TryParse(string s, out int result),该语句返回的是bool值,指示转换操作是否成功,参数result是存放转换结果的变量。

例如:
string str = string.Empty;
str = "123";
int result=int.Parse(str);

string str = string.Empty;
str = "xyz";
int result;

int.TryParse(str, out result);

转自:http://www.cnblogs.com/xshy3412/archive/2007/08/29/874362.html


这些示例演示了一些用于将 string 转换为 int 的不同方法。 例如,当从命令行参数获取数值输入时,此类转换会很有用。 还存在一些将字符串转换为其他数值类型(如 float 或long)的类似方法。 下表列出了其中的一些方法。

此示例调用 ToInt32(String) 方法将输入的 string 转换为 int。 程序将捕捉此方法可能引发的两个最常见的异常。 如果该数字可以递增而不溢出整数存储位置,则程序使结果加上 1 并打印输出。

C#
                      int numVal = -1;
bool repeat = true;

while (repeat == true)
{
    Console.WriteLine("Enter a number between −2,147,483,648 and +2,147,483,647 (inclusive).");

    string input = Console.ReadLine();

    // ToInt32 can throw FormatException or OverflowException.try
    {
        numVal = Convert.ToInt32(input);
    }
    catch (FormatException e)
    {
        Console.WriteLine("Input string is not a sequence of digits.");
    }
    catch (OverflowException e)
    {
        Console.WriteLine("The number cannot fit in an Int32.");
    }
    finally
    {
        if (numVal < Int32.MaxValue)
        {
            Console.WriteLine("The new value is {0}", numVal + 1);
        }
        else
        {
            Console.WriteLine("numVal cannot be incremented beyond its current value");
        }
    }
    Console.WriteLine("Go again? Y/N");
    string go = Console.ReadLine();
    if (go == "Y" || go == "y")
    {
        repeat = true;
    }
    else
    {
        repeat = false;
    }
}
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();    


将 string 转换为 int 的另一种方法是使用 System.Int32 结构的 Parse 或 TryParse 方法。 ToUInt32 方法在内部使用 Parse。 如果字符串的格式无效,则 Parse 会引发一个异常;TryParse 不会引发异常,但会返回 false。 下面的示例演示了对 Parse 和 TryParse 的成功调用和不成功的调用。

C#
                      int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// Output: -105
C#
                      // TryParse returns true if the conversion succeeded
                      // and stores the result in the specified variable.
                      int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");
// Output: -105
C#
                      try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.
C#
                      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.
转自:http://msdn.microsoft.com/zh-cn/library/bb397679.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值