字符串中的数字自增,无数字字符最后加上001自增


/// <summary>
        /// 字符串格式的数据自增
        /// </summary>
        /// <param name="SelectDate">需要自增的字符串数据</param>
        /// <returns></returns>

public string StringFillSeries(string SelectDate)
        {

            var vRetStr = SelectDate;
            bool nonum = true;
            
            //判断选择的字符中是否包含数字
            for (int i = 0; i < SelectDate.Length; i++)
            {
                string s = SelectDate.Substring(SelectDate.Length - i - 1, 1);
                if (regex.IsMatch(s) == true)
                {
                    nonum = false;
                    break;
                }
            }
            if (nonum == true && 0 != SelectDate.Length)
            {
                vRetStr = vRetStr + "001";
                SelectDate = vRetStr;
                return SelectDate;
            }
            //else if (0 == SelectDate.Length)
            // {
            //     MessageBox.Show("字符为空");

            // }

            else if (1 == SelectDate.Length)
            {
                if (regex.IsMatch(SelectDate) == true)
                {
                    int i = Convert.ToInt32(SelectDate);
                    i++;
                    vRetStr = Convert.ToString(i);
                }
                else
                {
                    MessageBox.Show("字符串不可自增");
                }
            }
            else
            {
                for (int i = 1; i <= SelectDate.Length + 1; i++)
                {
                    //十进制插入字符(如果字符为9,自增变为零,前一位数加一,不为数字插入)
                    string vvIntChartwo = null;
                    //百进制插入字符(如果字符为19,自增变为零,前一位数加一,不为数字插入)
                    string vvIntCharthree = null;
                    // 将最后一个字符与之前的字符串分开,字符不是数字循环
                    string vOtherStr = SelectDate.Substring(0, SelectDate.Length - i);
                    //将为数字的字符转ASCII码值
                    int vIntCharone = (int)SelectDate[SelectDate.Length - i];
                    int vIntChartwo = 0;
                    int vIntCharthree = 0;

                    //  int vIntChar = (int)(SelectDate.Length - (SelectDate.Length - i));

                    if (SelectDate.Length > (i))
                    {
                        vIntChartwo = (int)SelectDate[SelectDate.Length - (i + 1)];
                    }
                    if (SelectDate.Length > (i + 1))
                    {
                        vIntCharthree = (int)SelectDate[SelectDate.Length - (i + 2)]; ;
                    }

                    //获取自增数字后的字符串
                    //int decollator = vRetStr.LastIndexOf(vOtherStr) + i - 1;
                    //string changestr = SelectDate.Substring(SelectDate.Length - decollator, decollator);

                    string changestr = SelectDate.Substring(SelectDate.Length - i + 1, i - 1);

                    if (48 <= vIntCharone && vIntCharone <= 57) //是数字(0 - 9)
                    {
                        vIntCharone++; //自增1
                        if (vIntCharone == 58) // 进一位
                        {
                            vIntCharone = 48;

                            if (48 <= vIntChartwo == false || vIntChartwo <= 57 == false) //不是是数字(0 - 9)
                            {
                                vvIntChartwo = "1";
                                vRetStr = vOtherStr + vvIntChartwo + (char)vIntCharone + changestr;
                                SelectDate = vRetStr;
                                return SelectDate;
                            }
                            else
                            {
                                vIntChartwo++;
                                vOtherStr = SelectDate.Substring(0, SelectDate.Length - (i + 1));
                                if (vIntChartwo == 58)
                                {
                                    vIntChartwo = 48;
                                    vOtherStr = SelectDate.Substring(0, SelectDate.Length - (i + 1));
                                    //if (SelectDate.Length > (i + 1) == false)
                                    //{
                                    //    vRetStr = vOtherStr + (char)vIntChartwo + (char)vIntCharone + changestr;
                                    //    SelectDate = vRetStr;
                                    //    return SelectDate;
                                    //}

                                    if (48 <= vIntCharthree && vIntCharthree <= 57 == false || vIntCharthree == 0) //不是是数字(0 - 9)
                                    {
                                        vvIntCharthree = "1";
                                        vRetStr = vOtherStr + vvIntCharthree + vvIntChartwo + (char)vIntChartwo + (char)vIntCharone + changestr;
                                        SelectDate = vRetStr;
                                        return SelectDate;
                                    }
                                    else
                                    {
                                        vIntCharthree++;
                                        vOtherStr = SelectDate.Substring(0, SelectDate.Length - (i + 2));
                                        vRetStr = vOtherStr + (char)vIntCharthree + (char)vIntChartwo + (char)vIntCharone + changestr;
                                        SelectDate = vRetStr;
                                        return SelectDate;
                                    }

                                }
                                vRetStr = vOtherStr + (char)vIntChartwo + (char)vIntCharone + changestr;
                                SelectDate = vRetStr;
                                return SelectDate;
                            }


                        }

                        vRetStr = vOtherStr + (char)vIntCharone + changestr;
                        SelectDate = vRetStr;
                        return SelectDate;
                    }

                }
            }
            SelectDate = vRetStr;
            return SelectDate;
        }

















特点字符串自增(例如www.jj_1)

 //“_”在字符串的位置,查询字符串中的数字
                //     int decollator, selnum;
                //     //需要改变的数字数据,改变之后的字符串值
                //     string changenum, changedata;

                //     //查询符合“_”在字符串的位置
                //     decollator = SelectDate.LastIndexOf("_") + 1;
                //     //获取字符串中的数字
                //     changenum = SelectDate.Substring(decollator, SelectDate.Length - decollator);
                //     //string 转换 int  要改变的数字数据
                //     int chgnum = Convert.ToInt32(changenum);
                //     for (int i = selectedRowIndex; i < dgvBatchTable.RowCount; i++)
                //     {
                //         changedata = SelectDate.Replace(changenum, string.Format("{0:000}",chgnum));
                //         dgvBatchTable.Rows[i].Cells[selectedColIndex].Value = changedata;
                //         chgnum++;
                //     }

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值