反转逗号分隔值字符串的顺序
Reverse the order of comma separated string of values
我有一串值,如下所示:
strSubjectIDs = "20,19,18,17,16,15";
是否可以颠倒字符串 strSubjectIDs
的顺序,使 ID 看起来像这样:
"15,16,17,18,19,20"
var reversedStr = string.Join(",", strSubjectIDs.Split(',').Reverse());
这是反转逗号分隔字符串元素的行业标准方法:
string strSubjectIDs = "20,19,18,17,16,15";
Console.WriteLine(strSubjectIDs);
Queue<Char> q = new Queue<Char>();
Stack<Queue<Char>> s = new Stack<Queue<Char>>();
foreach(Char c in (strSubjectIDs.Trim(',') + ",").ToCharArray())
{
q.Enqueue(c);
if (c == ',')
{
s.Push(q);
q = new Queue<char>();
}
}
while(s.Count > 0)
{
Queue<Char> t = s.Pop();
while(t.Count > 0)
{
q.Enqueue(t.Dequeue());
}
}
strSubjectIDs = new String(q.ToArray()).Trim(',');
Console.WriteLine(strSubjectIDs);
是的,当然;询问任何专业程序员:任何没有堆栈 and/or 队列的算法都不值得使用。
我有一串值,如下所示:
strSubjectIDs = "20,19,18,17,16,15";
是否可以颠倒字符串 strSubjectIDs
的顺序,使 ID 看起来像这样:
"15,16,17,18,19,20"
var reversedStr = string.Join(",", strSubjectIDs.Split(',').Reverse());
这是反转逗号分隔字符串元素的行业标准方法:
string strSubjectIDs = "20,19,18,17,16,15";
Console.WriteLine(strSubjectIDs);
Queue<Char> q = new Queue<Char>();
Stack<Queue<Char>> s = new Stack<Queue<Char>>();
foreach(Char c in (strSubjectIDs.Trim(',') + ",").ToCharArray())
{
q.Enqueue(c);
if (c == ',')
{
s.Push(q);
q = new Queue<char>();
}
}
while(s.Count > 0)
{
Queue<Char> t = s.Pop();
while(t.Count > 0)
{
q.Enqueue(t.Dequeue());
}
}
strSubjectIDs = new String(q.ToArray()).Trim(',');
Console.WriteLine(strSubjectIDs);
是的,当然;询问任何专业程序员:任何没有堆栈 and/or 队列的算法都不值得使用。