Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this</p> <pre><code>var source = GetTheString(); var reversed = source.Reverse().Select(x =&gt; x.ToString()).Aggregate((x,y) =&gt; x + "." + y); </code></pre> <p><strong>EDIT</strong></p> <p>This solution is definitely aimed at the "clever" end. It's likely much more performant to use a StringBuilder to build up the string. This solution creates many intermediate strings. </p> <p><strong>EDIT2</strong></p> <p>There was some debate about the relative speed of the "clever" solution vs. the StringBuilder approach. I wrote up a quick benchmark to measure the approach. As expected, StringBuilder is faster. </p> <ul> <li>Normal Aggregate (100 elements): 00:00:00.0418640</li> <li>WithStringBuilder (100 elements): 00:00:00.0040099</li> <li>Normal Aggregate (1000 elements): 00:00:00.3062040</li> <li>WithStringBuilder (1000 elements): 00:00:00.0405955</li> <li>Normal Aggregate (10000 elements): 00:00:03.0270392</li> <li>WithStringBuilder (10000 elements): 00:00:00.4149977 </li> </ul> <p>However, whether or not the speed difference is signficant is highly dependent upon where it is actually used in your application. </p> <p>Code for the benchmark. </p> <pre><code>public static class AggregateUnchanged { public static string Run(string input) { return input .Reverse() .Select(x =&gt; x.ToString()) .Aggregate((x, y) =&gt; x + "." + y); } } public static class WithStringBuilder { public static string Run(string input) { var builder = new StringBuilder(); foreach (var cur in input.Reverse()) { builder.Append(cur); builder.Append('.'); } if (builder.Length &gt; 0) { builder.Length = builder.Length - 1; } return builder.ToString(); } } class Program { public static void RunAndPrint(string name, List&lt;string&gt; inputs, Func&lt;string, string&gt; worker) { // Test case. JIT the code and verify it actually works var test = worker("123456"); if (test != "6.5.4.3.2.1") { throw new InvalidOperationException("Bad algorithm"); } var watch = new Stopwatch(); watch.Start(); foreach (var cur in inputs) { var result = worker(cur); } watch.Stop(); Console.WriteLine("{0} ({2} elements): {1}", name, watch.Elapsed, inputs.Count); } public static string NextInput(Random r) { var len = r.Next(1, 1000); var builder = new StringBuilder(); for (int i = 0; i &lt; len; i++) { builder.Append(r.Next(0, 9)); } return builder.ToString(); } public static void RunAll(List&lt;string&gt; input) { RunAndPrint("Normal Aggregate", input, AggregateUnchanged.Run); RunAndPrint("WithStringBuilder", input, WithStringBuilder.Run); } static void Main(string[] args) { var random = new Random((int)DateTime.Now.Ticks); RunAll(Enumerable.Range(0, 100).Select(_ =&gt; NextInput(random)).ToList()); RunAll(Enumerable.Range(0, 1000).Select(_ =&gt; NextInput(random)).ToList()); RunAll(Enumerable.Range(0, 10000).Select(_ =&gt; NextInput(random)).ToList()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload