我希望能够以“000000”开头的字符串长度为6个字符。然后我想把它增加一个'000001'当我点击9我想要去'00000a'当我到达z我想要去'00000A'。当达到'Z'时,我想将第一个重置为0并从下一个位置'000010'开始,依此类推。 '000011','000012'...'0000a0','0000a1'...'0000A0','0000A1'
我如何在C#中执行此操作?
提前谢谢你。 麦克
答案 0 :(得分:1)
这使用IntToString
支持问题Quickest way to convert a base 10 number to any base in .NET?中的任意基础,但硬编码使用您的格式(基础62)。
private static readonly char[] baseChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private const long targetBase = 62;
private const long maxNum = 62L*62*62*62*62*62 - 1;
public static string NumberToString(long value)
{
if (value > maxNum)
throw new ArgumentException();
char[] result = "000000".ToCharArray();
int i = result.Length - 1;
do
{
result[i--] = baseChars[value % targetBase];
value /= targetBase;
}
while (value > 0);
return new string(result);
}
答案 1 :(得分:1)
基于字符串的方法,类似于Tim S的回答:
private static string Increment(string input)
{
var myChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
var array = input.ToCharArray();
for (int i = array.Count()-1; i >= 0; i--)
{
int newValue = (Array.IndexOf(myChars, array[i]) + 1) % 62;
array[i] = myChars[newValue];
if (newValue != 0)
break;
}
return new string(array);
}
答案 2 :(得分:0)
这是另一种方法......它允许你传入一个"开始修订" (" 000000"在示例中)。很久以前,我最初在VB.Net中编写它,以回应具有非常具体要求的问题......所以下面可能不是最有效的做事方式。
public partial class Form1 : Form
{
private Revision rev;
public Form1()
{
InitializeComponent();
Reset();
}
private void Reset()
{
rev = new Revision("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "000000");
label1.Text = rev.CurrentRevision;
}
private void btnReset_Click(object sender, EventArgs e)
{
Reset();
}
private void btnNext_Click(object sender, EventArgs e)
{
rev.NextRevision();
if (rev.CurrentRevision.Length == 7)
{
MessageBox.Show("Sequence Complete");
Reset();
}
else
{
label1.Text = rev.CurrentRevision;
}
}
}
public class Revision
{
private string chars;
private char[] values;
private System.Text.StringBuilder curRevision;
public Revision()
{
this.DefaultRevision();
}
public Revision(string validChars)
{
if (validChars.Length > 0)
{
chars = validChars;
values = validChars.ToCharArray();
curRevision = new System.Text.StringBuilder(values[0]);
}
else
{
this.DefaultRevision();
}
}
public Revision(string validChars, string startingRevision)
: this(validChars)
{
curRevision = new System.Text.StringBuilder(startingRevision.ToUpper());
int i = 0;
for (i = 0; i <= curRevision.Length - 1; i++)
{
if (Array.IndexOf(values, curRevision[i]) == -1)
{
curRevision = new System.Text.StringBuilder(values[0]);
break;
}
}
}
private void DefaultRevision()
{
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
values = chars.ToCharArray();
curRevision = new System.Text.StringBuilder(values[0]);
}
public string ValidChars
{
get { return chars; }
}
public string CurrentRevision
{
get { return curRevision.ToString(); }
}
public string NextRevision(int numRevisions = 1)
{
bool forward = (numRevisions > 0);
numRevisions = Math.Abs(numRevisions);
int i = 0;
for (i = 1; i <= numRevisions; i++)
{
if (forward)
{
this.Increment();
}
else
{
this.Decrement();
}
}
return this.CurrentRevision;
}
private void Increment()
{
char curChar = curRevision[curRevision.Length - 1];
int index = Array.IndexOf(values, curChar);
if (index < (chars.Length - 1))
{
index = index + 1;
curRevision[curRevision.Length - 1] = values[index];
}
else
{
curRevision[curRevision.Length - 1] = values[0];
int i = 0;
int startPosition = curRevision.Length - 2;
for (i = startPosition; i >= 0; i += -1)
{
curChar = curRevision[i];
index = Array.IndexOf(values, curChar);
if (index < (values.Length - 1))
{
index = index + 1;
curRevision[i] = values[index];
return;
}
else
{
curRevision[i] = values[0];
}
}
curRevision.Insert(0, values[0]);
}
}
private void Decrement()
{
char curChar = curRevision[curRevision.Length - 1];
int index = Array.IndexOf(values, curChar);
if (index > 0)
{
index = index - 1;
curRevision[curRevision.Length - 1] = values[index];
}
else
{
curRevision[curRevision.Length - 1] = values[values.Length - 1];
int i = 0;
int startPosition = curRevision.Length - 2;
for (i = startPosition; i >= 0; i += -1)
{
curChar = curRevision[i];
index = Array.IndexOf(values, curChar);
if (index > 0)
{
index = index - 1;
curRevision[i] = values[index];
return;
}
else
{
curRevision[i] = values[values.Length - 1];
}
}
curRevision.Remove(0, 1);
if (curRevision.Length == 0)
{
curRevision.Insert(0, values[0]);
}
}
}
}