要实现的功能看起来很简单,但是想了几天也没想出个好办法,大家帮我看看有没好方法要实现功能和Excel的“以序列方式填充”类似,比如:
有一个字符串
A100
提高点数是1,将生成,字符串长度不变,如下:
A100
A101
A102
...
A999再比如:
汉字1 汉1字
汉字2 汉2字
汉字3 汉3字
汉字4 汉4字
汉字5 汉5字
汉字6 汉6字
汉字7 汉7字
汉字8 汉8字
汉字9 汉9字
全角数字也能行,Excel这个功能好强,我在写一个造测试数据用的程序,需要给表中的字段,自动填充类似的值,只需要指定开始字符串和点数,就能生成上述类似的结果。求一种好的方法
有一个字符串
A100
提高点数是1,将生成,字符串长度不变,如下:
A100
A101
A102
...
A999再比如:
汉字1 汉1字
汉字2 汉2字
汉字3 汉3字
汉字4 汉4字
汉字5 汉5字
汉字6 汉6字
汉字7 汉7字
汉字8 汉8字
汉字9 汉9字
全角数字也能行,Excel这个功能好强,我在写一个造测试数据用的程序,需要给表中的字段,自动填充类似的值,只需要指定开始字符串和点数,就能生成上述类似的结果。求一种好的方法
原理简单,实现起来比较难
学习ing
{
string s1 = m.Value;
string s = "";
bool isAdd = true;
char h1 = '1', H1 = (char)65297;
int n1 = 0; for(int i=s1.Length - 1;i>=0;i--)
{
char c1 = s1[i];
char c;
if(!isAdd)
{
s = s.Insert(0, c1.ToString());
continue;
}
n1 = (int)c1;
if(n1==57 || n1==65305)
{
c = (char)(n1 + 1 - 10);
isAdd = true;
}
else
{
c = (char)(n1 + 1);
isAdd = false;
}
s = s.Insert(0, c.ToString());
}
if(isAdd && n1!=0)
{
s = s.Insert(0, (n1>=48 && n1<=57)?h1.ToString():H1.ToString());
}
return s;
}string s = "字12字2字23字929字9";
Regex reg = new Regex(@"(\d+)");
s = reg.Replace(s, new MatchEvaluator(this.ValueAdd));
Console.WriteLine(s);
s = reg.Replace(s, new MatchEvaluator(this.ValueAdd));
Console.WriteLine(s);
......输出结果:
字13字3字24字930字10
字14字4字25字931字11
......
using System.IO;
using System.Text.RegularExpressions;public class Sample
{
public static void Main()
{
string s = "汉字2123中文234中1";
for(int i = 0; i < 10; i++)
{
Console.WriteLine(s = Next(s));
}
}
private static string Next(string s)
{
string n = null;
int p = 0;
foreach(Match m in Regex.Matches(s, "[0-9]+"))
{
n += s.Substring(p, m.Index - p);
n += (Int32.Parse(m.Value) + 1);
p = m.Index + m.Length;
}
return n + s.Substring(p);
}
}
using System.Text.RegularExpressions;public class Sample
{
public static void Main()
{
string s = "汉字2193中文234中11";
for(int i = 0; i < 20; i++)
{
Console.WriteLine(s = Next(s));
}
}
private static string Next(string s)
{
string n = null;
int p = 0;
foreach(Match m in Regex.Matches(s, @"\d+"))
{
n += s.Substring(p, m.Index - p) + NextNum(m.Value);
p = m.Index + m.Length;
}
return n + s.Substring(p);
}
private static string NextNum(string s)
{
char [] cs = s.ToCharArray();
for(int i = s.Length - 1; i >=0; i--)
{
if(!NextChar(ref cs[i])) break;
}
return new string(cs);
}
private static bool NextChar(ref char c)
{
string p = "01234567890123456789";
int n = p.IndexOf(c);
c = p[(n + 1) % 10 + 10 * (n / 10)];
return (n == 9 || n == 19);
}
}
代码如下Private Function GetNext(ByVal str As String, ByVal [step] As Integer) As String
Dim s As String = String.Empty
Dim mc As MatchCollection = Regex.Matches(str, "\d+")
Dim m As Match
Dim p As Integer = 0 If mc.Count > 0 Then
m = mc.Item(mc.Count - 1)'只取最后一组数字 Dim stmp As String = m.Value
For i As Integer = 1 To [step]
stmp = GetNextNum(stmp)
Next
s = str.Substring(0, m.Index) & stmp p = m.Index + m.Length
End If Return s & str.Substring(p)
End Function Private Function GetNextNum(ByVal str As String) As String
Dim ch() As Char = str.ToCharArray()
For i As Integer = str.Length - 1 To 0 Step -1
If Not GetNextChar(ch(i)) Then
Exit For
End If
Next
Return New String(ch)
End Function Private Function GetNextChar(ByRef ch As Char) As Boolean
Dim As String = "01234567890123456789"
Dim index As Integer = .IndexOf(ch)
ch = .Substring((index + 1) Mod 10 + 10 * (index \ 10), 1)
Return index = 9 Or index = 19
End Function
生成的结果是
A0
A1
A2
A3
A4
A5
A6
A7
A8
A9
A0
而不是
A0
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
不过对于我的程序不要紧~~
private string num(string start,int step)
{
int iStep=step;
string Start=start;
string StartNew=Start;
string StartOld=Start;
string tmp="";
string Flag=""; string[] num=new string [] {"0","1","2","3","4","5","6","7","8","9"};
string[] num_All=new string [] {"0","1","2","3","4","5","6","7","8","9"}; string num_AllStr="0123456789";
for(int i=0;i<num_All.Length;i++)
{
Start=Start.Replace(num_All[i],num[i]);
Flag=Flag+"0";
} Regex reg = new Regex(@"(\d+)");
Regex regCh =new Regex(@"(\D+)");
Match mat=reg.Match(Start);
Match matCh=regCh.Match(Start);
while(mat.Success)
{
if(mat!=null)
{
int iOld=Convert.ToInt32(mat.Value);
int iNew=iOld+iStep; string tmpDig=StartOld.Substring(mat.Index,mat.Length);
string sNew=""; if(iOld.ToString().Length<iNew.ToString().Length)//有进位
{
if(num_AllStr.IndexOf(tmpDig.Substring(0,1))!=-1)//下一位为全角字符
{
for(int k=0;k<iNew.ToString().Length-iOld.ToString().Length;k++)
{
sNew=sNew+num_All[int.Parse(iNew.ToString().Substring(k,1))];
}
}
else//下一位为非全角字符
{
sNew=sNew+iNew.ToString().Substring(0,iNew.ToString().Length-iOld.ToString().Length);
}
for(int j=0;j<tmpDig.Length;j++)
{
if(num_AllStr.IndexOf(tmpDig.Substring(j,1))!=-1)
{
sNew=sNew+num_All[int.Parse(iNew.ToString().Substring(j+iNew.ToString().Length-iOld.ToString().Length,1))];
}
else
{
sNew=sNew+num[int.Parse(iNew.ToString().Substring(j+(iNew.ToString().Length-iOld.ToString().Length),1))];
}
}
}
else//没有进位
{
for(int j=0;j<tmpDig.Length;j++)
{
if(num_AllStr.IndexOf(tmpDig.Substring(j,1))!=-1)
{
sNew=sNew+num_All[int.Parse(iNew.ToString().Substring(j,1))];
}
else
{
sNew=sNew+num[int.Parse(iNew.ToString().Substring(j,1))];
}
}
}
if(mat.Index<matCh.Index)
{
//tmp=tmp+iNew.ToString()+matCh.Value;
tmp=tmp+sNew+matCh.Value;
}
else
{
tmp=tmp+matCh.Value+sNew;
}
mat=mat.NextMatch();
matCh=matCh.NextMatch();
}
}
if(matCh.Success)
{
tmp=tmp+matCh.Value;
}
return tmp;
}//主函数
for(int i=0;i<总数;i++)
{
MessageBox.Show(num("字13字3字24字930字10",步长*(i+1));
}