“00001”把这个字符串转换为int型为1 再加1 为2, 怎么能变成“00002” (C#)

除了补零,有没有能具体实现的代码

既然你来求助我就告诉你一个最快的方法,只是性能最快,别的方法都是循环补零,都有消耗!
--------------------------------
“1”+“00001”
这样转化就变成 100001
此时加 1
变成100002
变成字符串然后从第二位取字符串 就变成了“00002”
------------------------------------
以上方式不用循环补零,据我目前的经验应该是最快的,也许别的高人有比我更快的方式。
如果害怕发生进位,就最换成一些较大的开头 比如 1000+00001
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-12
string a="2";
string b =a.Insert(0,"0000");追问

你这种方法 只能是固定的值,要是加到10时就应该少个0 了啊

追答

string a = 1;
if (a.Length<6)
{
for (int i = 0; i < (6-a.Length); i++)
{
a = a.Insert(0, "0");
}

}
这样最合理

第2个回答  推荐于2016-09-02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "00001";
int i = Int32.Parse(s);
i++;
string ss = i.ToString().PadLeft(5, '0');
Console.WriteLine(ss);

}
}
}
写把字符串转换为整型,然后加1,再转换成字符串,用padleft函数在前面加上0.也可以直接用格式化字符串本回答被提问者采纳
相似回答