0
保存之前我必須處理唯一的名稱,並將其保存在數據庫一樣 -找到唯一的名稱在C#和SQL Server
如果名稱已經存在,找到可以追加到該名稱的最小數目將其保存爲一個獨特的名字。
例如: 如果用戶名Scott已存在,則另存爲Scott(1)。如果斯科特(1)已經存在,保存爲斯科特(2)。等
我使用C#和SQL Server 2010
任何偉大的想法?
保存之前我必須處理唯一的名稱,並將其保存在數據庫一樣 -找到唯一的名稱在C#和SQL Server
如果名稱已經存在,找到可以追加到該名稱的最小數目將其保存爲一個獨特的名字。
例如: 如果用戶名Scott已存在,則另存爲Scott(1)。如果斯科特(1)已經存在,保存爲斯科特(2)。等
我使用C#和SQL Server 2010
任何偉大的想法?
想通了這一點 - 但欣賞改進
var seprator = "scott";
var list = new List<string>();
list.Add("scott");
list.Add("scott(1)");
list.Add("scott Alex");
list.Add("scott (4)");
list.Add("scott(xxx)");
list.Add("scott(250)");
list.Add("scott(12s)");
list.Add("scott(123)x");
list.Add("Scott caps");
list.Add("Alex Scott caps");
list.Add("xxxscottmmm");
var numberList = new List<int>();
foreach (var v in list)
{
var parts = Regex.Split(v, seprator, RegexOptions.IgnoreCase);
if (parts.Length > 1) //(1) Alex (4) (xxx) (250) (12s) (123)x caps caps
{
var secondPart = parts[1].Trim();
if (secondPart.StartsWith("(") && secondPart.EndsWith(")")) // (1) (4) (xxx) (250) (12s)
{
var valuebetweenbraces = secondPart.Substring(1, secondPart.Length - 2); //1 4 xxx 250 12s
int number;
var isNumber = int.TryParse(valuebetweenbraces, out number);
if (isNumber)
{
numberList.Add(number);
}
}
}
}
int maxValue = 0;
if (numberList.Count > 0)
maxValue = numberList.Max() + 1;
else
maxValue = maxValue + 1;
Response.Write(seprator + "(" + maxValue + ")");
什麼編程語言和數據庫類型是您使用? – Trevor