/// <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++;
// }
字符串中的数字自增,无数字字符最后加上001自增
于 2023-04-04 15:53:09 首次发布