1、int–>string

 int a = 15;
 string s1 = a.tostring();
 string s2 = convert.tostring(a);

2、string –>int

 string s = "18";
 int a1 = int.parse(s);
 int a2;
 int.tryparse(s, out a2);
 int a3 = convert.toint32(s);

总结:

1、可以使用convert对int,string进行来回转化,并且可以指定转化的进制;

2、转化为string,可以使用tostring方法;

3、转化为int,可以使用int.parse或者int.tryparse方法。

为什么没有string.parse和string.tryparse方法?不需要,tostring就可以了。

补充知识:c#控制台输出退格实现变换闪烁的字符效果

c#控制台输出退格实现变换闪烁的字符效果,传统的console.clear()方法能清除控制台上的所有内容。

如果用 console.write(‘\u0008’);可以实现输出退格,这样就可以方便地清除某一个或者某几个字符内容。

实例如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.threading;
 
namespace consoleapplication2
{
 class program
 {
  static void main(string[] args)
  {
   console.write("number : ");
   for (int i = 0; i < 10000; i++)
   {
    switch (i)
    {
     case 10: flashingstring(i, 1); break;
     case 100: flashingstring(i, 2); break;
     case 1000: flashingstring(i, 3); break;
     case 10000: flashingstring(i, 4); break;
     default: flashingstring(i); break;
    }
    thread.sleep(100); //间歇变换
   }
   console.readline();
  }
 
  //显示变换闪烁的字符方法
  public static void flashingstring(int num, int backlength = 0)
  {
   if (num > 0)
   {
    if (backlength <= 0) backlength = num.tostring().length;
    // 清除旧的字符
    for (int i = 0; i < backlength; i++)
    {
     //输出退格
     console.write('\u0008');
    }
   }
   // 输出新字符
   console.write(num);
  }
 }
}

以上这篇c#开发之int与string转化操作就是www.887551.com分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持www.887551.com。