字符串是数字并且前面有可能“0”的自动累加例子

来源:互联网 发布:java开发实例 编辑:程序博客网 时间:2024/06/07 01:44

需求:   starid=0001,      

               endid=0003.

希望能在插入的时候分别插入:0001,0002,0003

starid,endid  可能是 0000001,也可能是:01,也可能是:1 也就前面的0不定是多少位,可能有可能没有!


例子:

                string sbarcode = starid;//得到值
                string encode = endid;
              


                long starb = Convert.ToInt64(sbarcode );//转换类型
                long endb = Convert.ToInt64(encode );
                long poor = endb - starb;//相减
                string Fsbarcode = sbarcode.Substring(0, 1);//判断字符串第一个字符是不是0
                int bl = 0;
                if (Fsbarcode=="0")
                {
                    bl = 1;
                }

         if(starb>endb)//如果开始值大于结束值不执行操作
            {
                Showmsgs("Please enter the correct barcode!");
                return;
            }


            if(Fsbarcode=="0")//如果前面有  0
            {
                if(sbarcode.Length!=encode.Length)//但是开始和结束值长度不一样不执行
                {
                    Showmsgs("Please enter the correct barcode!");
                    return;
                }


            }


                int leng = sbarcode.Length;//获得长度
                if (poor == 0)//两个值一样就值执行一次添加记录
                {
                  
                    //...............执行添加一条操作
                }
                else
                {
                    string aa = "";//声明一个临时变量
                    poor += 1;//将 相差值加1 是为了方便后面循环
                    for (int i = 0; i < poor; i++)
                    {
                        if (bl == 1)//前面有 0的
                        {
                            aa = Convert.ToString(starb).PadLeft(leng, '0');
                        }else
                        {
                            aa =Convert.ToString(starb);
                        }

                         // 执行添加语句  aa 是对应的ID值
                       
                        starb += 1;//将开始ID加1


                    }