您可以使用LINQ或LAMBDA表达式对一行中的字符串进行逆序处理

并不是说我想要实际使用它(出于很多原因),但出于严格的好奇心,我想知道是否有一种方法可以在一行代码中使用LINQ和/或LAMBDA表达式对字符串进行逆序,而不使用任何代码框架“反向”方法。

例如

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

和reversedValue将导致“em esrever”

编辑显然,一个不切实际的问题/解决方案我知道这一点,所以不要担心它严格地是围绕LINQ / LAMBDA结构的好奇心问题。

我没有看到这方面的实际用途,只是为了好玩:

 new string(Enumerable.Range(1, input.Length).Select(i => input[input.Length - i]).ToArray()) 

好吧,即使不使用LINQ或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); 

(亲爱的潜在编辑:不要将它打开到多行。整点是它是单行,按照它上面的句子和问题。)

但是,如果我看到任何人为了它而避免使用框架方法,我会质疑他们的理智。

请注意,这根本不使用LINQ。 LINQ的答案是:

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

避免使用Reverse,而是使用OrderByDescending:

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

布莱什。 我喜欢Mehrdad的回答。 当然,所有这些都远比简单方法效率低得多。

哦,他们也都错了。 反转字符串比反转代码点的顺序更复杂。 考虑组合字符,代理对等...

 new string(value.Reverse().ToArray()) 
 var reversedValue = value.ToCharArray() .Select(ch => ch.ToString()) .Aggregate((xs, x) => x + xs); 

具有递归lambda的变体:

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

LP,Dejan

您可以使用Aggregate将每个Char到反向字符串:

  "reverse me".Aggregate("", (acc, c) => c + acc); 
 var reversedValue= "reverse me".Reverse().ToArray(); 

除了之前的一篇文章,这里还有一个更高效的解决方案。

 var actual0 = "reverse me".Aggregate(new StringBuilder(), (x, y) => x.Insert(0, y)).ToString(); 
 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; } 

非常简单。 所以,从现在开始,我有一个方法可以反转一个字符串,它不使用任何内置的反向函数。

所以在你的主要方法中,去吧,

Console.WriteLine(Reverse(“Some word”));

从技术上讲,这是你的一个class轮:P

如果我们需要支持组合字符和代理对:

 // This method tries to handle: // (1) Combining characters // These are two or more Unicode characters that are combined into one glyph. // For example, try reversing "Not nai\u0308ve.". The diaresis (¨) should stay over the i, not move to the v. // (2) Surrogate pairs // These are Unicode characters whose code points exceed U+FFFF (so are not in "plane 0"). // To be represented with 16-bit 'char' values (which are really UTF-16 code units), one character needs *two* char values, a so-called surrogate pair. // For example, try "The sphere \U0001D54A and the torus \U0001D54B.". The 𝕊 and the 𝕋 should be preserved, not corrupted. var value = "reverse me"; // or "Not nai\u0308ve.", or "The sphere \U0001D54A and the torus \U0001D54B.". var list = new List(value.Length); var enumerator = StringInfo.GetTextElementEnumerator(value); while (enumerator.MoveNext()) { list.Add(enumerator.GetTextElement()); } list.Reverse(); var result = string.Concat(list); 

文档: MSDN: System.Globalization.StringInfo

 string str="a and b"; string t=""; char[] schar = str.Reverse().ToArray(); foreach (char c in schar ) { test += c.ToString(); }