Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you reverse order a string in one line with LINQ or a LAMBDA expression

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.

like image 216
Student for Life Avatar asked Jul 20 '09 10:07

Student for Life


People also ask

How do you reverse a string using lambda expression?

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.

Which is faster LINQ or Lambda?

There is no performance difference between LINQ queries and Lambda expressions.

How to get the reversed value of a string in LINQ?

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 ().

Is it possible to reverse the Order of a string?

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!

How to implement comparator with two strings?

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:

How do I get the reverse value of a string?

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.


1 Answers

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...

like image 165
Jon Skeet Avatar answered Oct 02 '22 22:10

Jon Skeet