Can you reverse order a string in one line with LI

2020-05-20 08:17发布

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.

11条回答
The star\"
2楼-- · 2020-05-20 08:49

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

 "reverse me".Aggregate("", (acc, c) => c + acc);
查看更多
Emotional °昔
3楼-- · 2020-05-20 08:50
public static string Reverse(string word)
{
   int index = word.Length - 1;
   string reversal = "";

   //for each char in word
   for (int i = index; index >= 0; index--)
   {
       reversal = reversal + (word.Substring(index, 1));
       Console.WriteLine(reversal);
   }
   return reversal;
}

Quite simple. So, from this point on, I have a single method that reverses a string, that doesn't use any built-in Reverse functions.

So in your main method, just go,

Console.WriteLine(Reverse("Some word"));

Technically that's your one liner :P

查看更多
Fickle 薄情
4楼-- · 2020-05-20 08:59
var reversedValue= "reverse me".Reverse().ToArray();
查看更多
甜甜的少女心
5楼-- · 2020-05-20 09:00

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

查看更多
6楼-- · 2020-05-20 09:04

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

查看更多
对你真心纯属浪费
7楼-- · 2020-05-20 09:10

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())
查看更多
登录 后发表回答