views:

752

answers:

6

Not that I would want to use this practically (for many reasons) but out of strict curiousity I would like to know if there is a way to reverse order a string using LINQ and/or LAMBDA expressions in one line of code, without utilising any framework "Reverse" methods.

e.g.

string value = "reverse me";
string reversedValue = (....);

and reversedValue will result in "em esrever"

EDIT Clearly an impractical problem/solution I know this, so don't worry it's strictly a curiosity question around the LINQ/LAMBDA construct.

+10  A: 

I don't see a practical use for this but just for the sake of fun:

new string(Enumerable.Range(1, input.Length).Select(i => input[input.Length - i]).ToArray())
Mehrdad Afshari
@Mehrdad, don't worry I wouldn't use it practically but was trying to satisfy personal curiosity.
Student for Life
+6  A: 

Well, I can do it in one very long line, even without using LINQ or a lambda:

string original = "reverse me"; char[] chars = original.ToCharArray(); char[] reversed = new char[chars.Length]; for (int i=0; i < chars.Length; i++) reversed[chars.Length-i-1] = chars[i]; string reversedValue = new string(reversed);

However, if I saw anyone avoiding using framework methods for the sake of it, I'd question their sanity.

Note that this doesn't use LINQ at all. A LINQ answer would be:

string reverseValue = new string(original.Reverse().ToArray());

Avoiding using Reverse, but using OrderByDescending instead:

string reverseValue = new string(original.Select((c, index) => new { c, index })
                                         .OrderByDescending(x => x.index)
                                         .ToArray());

Blech. I like Mehrdad's answer though. Of course, all of these are far less efficient than the straightforward approach.

Oh, and they're all wrong, too. Reversing a string is more complex than reversing the order of the code points. Consider combining characters, surrogate pairs etc...

Jon Skeet
Crap. I had an off by one error two times before I did it right, for such an easy problem. I'd avoid my approach at all costs!
Mehrdad Afshari
I don't know if it's just me, but I don't consider it a one liner if a statement terminator ; is used and another statement appearing straight after it.
Josh Smeaton
@Josh: Given that the whole question is somewhat ridiculous IMO, I was taking it absolutely literally. Put it this way - if you went to the line number containing each statement in that code, how many different values would you get? It just shows the value in being precise.
Jon Skeet
@Jon, the question was edited to indicate "curiosity" as I wouldn't be using this idea anywhere as I know its an impractical concept. I just wanted to find a non obvious way to perform a known operation and then was thinking of sharing it with my team to see if they could figure it out, although i have now decided to scrap the idea.
Student for Life
+1  A: 

Variant with recursive lambda:

  var value = "reverse me";
  Func<String, String> f = null; f = s => s.Length == 1 ? s : f(s.Substring(1)) + s[0]; 
  var reverseValue = f(value);

LP, Dejan

Dejan Stanič
+4  A: 
var reversedValue = value.ToCharArray()
                         .Select(ch => ch.ToString())
                         .Aggregate<string>((xs, x) => x + xs);
Joe Chung
+1  A: 

You can use Aggregate to prepend each Char to the reversed string:

 "reverse me".Aggregate("", (acc, c) => c + acc);
Ben Lings
A: 

new string(value.Reverse().ToArray())

shlomiw