I have a string of values that looks like this:
strSubjectIDs = "20,19,18,17,16,15";
Is there anyway to reverse that order of the string strSubjectIDs
so that the IDs look like this:
"15,16,17,18,19,20"
This is the industry standard way to reverse the elements of a comma delimited string:
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);
Yeah, definitely; ask any professional programmer: Any algorithm not having a stack and/or a queue just isn't worth using.