Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:44:27+00:00 2026-05-11T14:44:27+00:00

Here’s a simple problem. I have an application that takes a phone number like

  • 0

Here’s a simple problem. I have an application that takes a phone number like ‘13335557777’, and needs to reverse it and insert a dot between each number, like this:

‘7.7.7.7.5.5.5.3.3.3.1.’

I know I can do this with a StringBuilder and a for-loop to reverse the string and insert the dots, but is there a clever way to do this in LINQ (or some other way)?

Note: for this, I’m not really concerned with performance or memory allocation or whatever, just curious to see how this would be done in LINQ.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:44:28+00:00Added an answer on May 11, 2026 at 2:44 pm

    Try this

    var source = GetTheString(); var reversed = source.Reverse().Select(x => x.ToString()).Aggregate((x,y) => x + '.' + y); 

    EDIT

    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.

    EDIT2

    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.

    • Normal Aggregate (100 elements): 00:00:00.0418640
    • WithStringBuilder (100 elements): 00:00:00.0040099
    • Normal Aggregate (1000 elements): 00:00:00.3062040
    • WithStringBuilder (1000 elements): 00:00:00.0405955
    • Normal Aggregate (10000 elements): 00:00:03.0270392
    • WithStringBuilder (10000 elements): 00:00:00.4149977

    However, whether or not the speed difference is signficant is highly dependent upon where it is actually used in your application.

    Code for the benchmark.

    public static class AggregateUnchanged {     public static string Run(string input) {         return input             .Reverse()             .Select(x => x.ToString())             .Aggregate((x, y) => 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 > 0) {             builder.Length = builder.Length - 1;         }          return builder.ToString();     } }  class Program {     public static void RunAndPrint(string name, List<string> inputs, Func<string, string> 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 < len; i++) {             builder.Append(r.Next(0, 9));         }         return builder.ToString();     }      public static void RunAll(List<string> 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(_ => NextInput(random)).ToList());         RunAll(Enumerable.Range(0, 1000).Select(_ => NextInput(random)).ToList());         RunAll(Enumerable.Range(0, 10000).Select(_ => NextInput(random)).ToList());     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
Here's a coding problem for those that like this kind of thing. Let's see
Here's a problem I ran into recently. I have attributes strings of the form
Here's my scenario - I have an SSIS job that depends on another prior
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here we go again, the old argument still arises... Would we better have a
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.