很难说这是对的。基本上,我有一个List,我需要找回一个字符串,它将是字母顺序列表中的最后一个。例如:
如果我有清单:
{ "01", "0a2", "test" }
我需要按字母顺序取回下一个字符串,例如“tf”
答案 0 :(得分:1)
这有点像一个hacky答案,但我希望有所帮助。假设你的字符串在排序列表中,我有一个小巧的“快速而肮脏”的方法,它按字母顺序生成一个项目:
var items = new System.Collections.Generic.SortedList<string, string>();
items.Add("01", "01");
items.Add("02a", "02a");
items.Add("test", "test");
var nextItem = items.Last().Key;
int pos = nextItem.Length - 1;
while (pos >= 0)
{
if ((nextItem[pos] != 'z') && (nextItem[pos] != 'Z'))
{
nextItem = nextItem.Substring(0, pos - 1) + Convert.ToChar(Convert.ToInt32(nextItem[pos]) + 1) + nextItem.Substring(pos + 1);
break;
}
pos--;
}
if (pos == -1)
{
nextItem += "a";
}
答案 1 :(得分:0)
大多数语言都有数组排序功能,按字母顺序排序。只需进行排序,然后选择数组的最后一个元素。
答案 2 :(得分:0)
如果你总是想要一个在所有现有字符串之后按字母顺序“最后”的字符串,这应该有效;)
var words = new []{"01","0a2","test"};
int maxLength = words.ToList().Max(x => x.Length);
string newWord ="";
for(int i=0;i<maxLength +1;i++){
newWord+= "z";
}