C# 如何创建字母数字自动增量
有人能给我指导或至少给我一些教程链接吗?我想创建一个字母数字自动递增ID/代码,格式如下123-A0000010-A12。123和A12是常量,A0000010是自动递增的 我将使用一个简单的计数器递增,并使用第二个属性获取该数字,并输出一个带有ID格式的格式化字符串 你可以这样写:C# 如何创建字母数字自动增量,c#,.net,C#,.net,有人能给我指导或至少给我一些教程链接吗?我想创建一个字母数字自动递增ID/代码,格式如下123-A0000010-A12。123和A12是常量,A0000010是自动递增的 我将使用一个简单的计数器递增,并使用第二个属性获取该数字,并输出一个带有ID格式的格式化字符串 你可以这样写: int Counter= 100; string ID=String.Format("123-A{0}-A12",Counter); 我将使用一个简单的计数器递增,并使用第二个属性获取该数字并输出一个带有ID格
int Counter= 100;
string ID=String.Format("123-A{0}-A12",Counter);
我将使用一个简单的计数器递增,并使用第二个属性获取该数字并输出一个带有ID格式的格式化字符串 你可以这样写:
int Counter= 100;
string ID=String.Format("123-A{0}-A12",Counter);
你这样说:
private void bntOK_Click(object sender, EventArgs e)
{
int cnt10, ntEQ;
cnt10 = 10;
string id = null;
string dsp = txtInput.Text.Trim(' ');
int rs = string.Compare(dsp, "9");
if (rs == 0)
{
id = string.Format("123-000000{00}-A12", cnt10);
txtResult.Text = id;
}
else
{
int dspt = Convert.ToInt16(txtInput.Text.Trim());
ntEQ = dspt + 1;
id = string.Format("123-000000{00}-A12", ntEQ);
txtResult.Text = id;
}
}
你这样说:
private void bntOK_Click(object sender, EventArgs e)
{
int cnt10, ntEQ;
cnt10 = 10;
string id = null;
string dsp = txtInput.Text.Trim(' ');
int rs = string.Compare(dsp, "9");
if (rs == 0)
{
id = string.Format("123-000000{00}-A12", cnt10);
txtResult.Text = id;
}
else
{
int dspt = Convert.ToInt16(txtInput.Text.Trim());
ntEQ = dspt + 1;
id = string.Format("123-000000{00}-A12", ntEQ);
txtResult.Text = id;
}
}
对于一项简单的任务来说,可能有些过火了,但我想做一个练习: ID类
public class AlphaNumericID
{
private string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public int Alpha { get; protected set;}
public int Numeric { get; protected set; }
public int NumericLenght { get; protected set; }
public string KeyFront { get; protected set; }
public string KeyEnd { get; protected set; }
public AlphaNumericID(string keyFront, string keyEnd, int numericLength)
{
Alpha = 0;
Numeric = 1;
KeyFront = keyFront;
KeyEnd = keyEnd;
NumericLenght = numericLength;
}
public void Increment()
{
Numeric++;
if (Numeric == Math.Pow(10, NumericLenght))
{
Alpha++;
Numeric = 1;
if (Alpha == chars.Length)
throw new Exception("Overflow!");
}
}
public override string ToString()
{
return String.Format("{0}-{1}{2}-{3}", KeyFront, chars[Alpha], Numeric.ToString().PadLeft(NumericLenght, '0'), KeyEnd);
}
}
您可以这样使用:
int Counter= 100;
string ID=String.Format("123-A{0}-A12",Counter);
- 申报
var id = new AlphaNumericID("123", "A12", 8); //Will create 123-A00000001-A12
(虽然如果你知道你的数据库永远不会这么高,你可能想考虑使用一个较短的ID)
- 增量
id.Increment();
- 输出
id.ToString();
这种封装的好处是,您可以轻松地扩展和更改其内部实现,尽管它对于您的特定需求来说可能太大了。对于一项简单的任务来说,可能有些过火了,但我想做一个练习: ID类
public class AlphaNumericID
{
private string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public int Alpha { get; protected set;}
public int Numeric { get; protected set; }
public int NumericLenght { get; protected set; }
public string KeyFront { get; protected set; }
public string KeyEnd { get; protected set; }
public AlphaNumericID(string keyFront, string keyEnd, int numericLength)
{
Alpha = 0;
Numeric = 1;
KeyFront = keyFront;
KeyEnd = keyEnd;
NumericLenght = numericLength;
}
public void Increment()
{
Numeric++;
if (Numeric == Math.Pow(10, NumericLenght))
{
Alpha++;
Numeric = 1;
if (Alpha == chars.Length)
throw new Exception("Overflow!");
}
}
public override string ToString()
{
return String.Format("{0}-{1}{2}-{3}", KeyFront, chars[Alpha], Numeric.ToString().PadLeft(NumericLenght, '0'), KeyEnd);
}
}
您可以这样使用:
int Counter= 100;
string ID=String.Format("123-A{0}-A12",Counter);
- 申报
var id = new AlphaNumericID("123", "A12", 8); //Will create 123-A00000001-A12
(虽然如果你知道你的数据库永远不会这么高,你可能想考虑使用一个较短的ID)
- 增量
id.Increment();
- 输出
id.ToString();
这种封装的好处是,您可以轻松地扩展和更改其内部实现,尽管它对于您的特定需求来说可能太大了。示例数据?123-A9999999-A12之后会发生什么?哦,是的-你尝试过什么?这个网站是用来提问的,它不是一个免费的编码服务。只有你的程序才能管理ID生成?B00000001我想我的数据库无法达到这个目标far@Blorgbeard我不是要一个代码,我只是要一个想法或教程,如果有最好的。我已经有了一个想法,但两个脑袋总比一个好。样本数据?123-A9999999-A12之后会发生什么?哦,是的-你尝试过什么?这个网站是用来提问的,它不是一个免费的编码服务。只有你的程序才能管理ID生成?B00000001我想我的数据库无法达到这个目标far@Blorgbeard我不是要一个代码,我只是要一个想法或教程,如果有最好的。我已经有了一个想法,但两个头总比一个头好。对于099,你用了什么字符串。从099替换到100?你不能替换。你必须说int计数器=100;字符串ID=string.Format(“123-A{0}-A12”,计数器);如果希望099的值显示为123-A00000010-A12而不是123-A10-A12,则可能需要计数器.ToString(“00000000”)。您使用的字符串是什么?从099替换为100?不替换。你必须说int计数器=100;字符串ID=string.Format(“123-A{0}-A12”,计数器);如果你想让你的值显示为123-A00000010-A12,而不是123-A10-A12,那么你就要大摇大摆了bro@GeorgeofJungle非常欢迎你,你真厉害bro@GeorgeofJungle非常欢迎兄弟非常感谢你给我完整的代码不客气,但是,尽管代码很容易使用,您仍然应该查看代码本身并尝试理解正在发生的事情。这将帮助你学习,也许你自己也能找到改进的方法:)是的,我现在就做,谢谢你,我非常感谢你给我完整的代码。不客气,但是尽管代码很容易使用,你还是应该看看代码本身,试着理解发生了什么。这将帮助你学习,也许你自己也能找到改进的方法:)是的,我现在就做,谢谢