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.
A String is an object that represents a sequence of characters and immutable in Java. We can reverse a string entered by the user using the charAt() method of String class to extract characters from the string and append them in reverse order to reverse the entered string.
There is no performance difference between LINQ queries and Lambda expressions.
var reversedValue = value.ToCharArray () .Select (ch => ch.ToString ()) .Aggregate<string> ( (xs, x) => x + xs); This is the only answer that actually does it in one line with LINQ. Second that you don't need ToCharArray ().
Reversing a string is more complex than reversing the order of the code points. Consider combining characters, surrogate pairs etc... 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!
It can be implemented with a lambda expression that takes two strings as parameters (in your case) and return an integer (this is the requirement of the SAM defined in the Comparator interface). is a lambda expression that takes two strings (the type is inferred by the compiler), but it returns a string and not an integer, hence the error:
string reverseValue = new string (original.Select ( (c, index) => new { c, index }) .OrderByDescending (x => x.index) .Select (x => x.c) .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.
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);
(Dear potential editors: do not unwrap this onto multiple lines. The whole point is that it's a single line, as per the sentence above it and the question.)
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)
.Select(x => x.c)
.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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With