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

  • SEARCH
  • Home
  • 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 883373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:34:43+00:00 2026-05-15T12:34:43+00:00

Ok, I can certainly do this with some IndexOf, or even a Split(), but

  • 0

Ok, I can certainly do this with some IndexOf, or even a Split(), but I thought I’d throw this out as a bit of a performance teaser.

I have data – like 100K’s of these – in LastName,FirstName Mi and I need to make it FirstName Mi Lastname.

I think that SubString/IndexOf(‘,’) can do the job, but was hoping for a more elegant/performant suggestion.

Any better ideas?

  • 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. Editorial Team
    Editorial Team
    2026-05-15T12:34:44+00:00Added an answer on May 15, 2026 at 12:34 pm

    .Split is probably the fastest/most concise. However .IndexOf is surprisingly the fastest in small tests for this case (where we can rely on two commas and use LastIndexOf).

    Paste the code below into LINQPad to test for yourself:

    For 10,000,000 results (best indicator as early results could be highly variable) I get:

    Regex Time 00:00:54.1151103

    Split Time 00:00:21.6187375

    IndexOf Time 00:00:24.2403165

    For 1,000,000 results I get:

    Regex Time 00:00:03.6016272

    Split Time 00:00:01.5575928

    IndexOf Time 00:00:00.9774164

    For 100,000 results I get:

    Regex Time 00:00:00.2587501

    Split Time 00:00:00.1013721

    IndexOf Time 00:00:00.0980560

    void Main()  
    {  
        int count = 100000;  
        WithRegex(count);  
        WithSplit(count);  
        WithIndexOf(count);
    }  
    
    void WithRegex(int count)  
    {  
        Regex _commaRegex = new Regex(@",", RegexOptions.Compiled);  
        string[] names = Enumerable.Range(1,count)
            .Select(i => "first,last,middle" + i).ToArray();  
        List<string> newNames = new List<string>(count);  
    
        Stopwatch stopWatch = new Stopwatch(); 
        stopWatch.Start();  
        foreach (string name in names)  
        {  
            string[] split = _commaRegex.Split(name);  
            StringBuilder sb = new StringBuilder();  
            sb.Append(split[0]).Append(split[2]).Append(split[1]);  
            newNames.Add(sb.ToString());  
        } 
        stopWatch.Stop(); 
        stopWatch.Elapsed.Dump("Regex Time");  
    }  
    
    void WithSplit(int count)  
    {  
        string[] names = Enumerable.Range(1,count)
             .Select(i => "first,last,middle" + i).ToArray();  
        List<string> newNames = new List<string>(count);  
    
        Stopwatch stopWatch = new Stopwatch(); 
        stopWatch.Start();  
        foreach (string name in names)  
        {  
            string[] split = name.Split(',');  
            StringBuilder sb = new StringBuilder();  
            sb.Append(split[0]).Append(split[2]).Append(split[1]);  
            newNames.Add(sb.ToString());  
        }  
        stopWatch.Stop(); 
        stopWatch.Elapsed.Dump("Split Time");  
    }  
    
    void WithIndexOf(int count)  
    {  
        string[] names = Enumerable.Range(1,count)
            .Select(i => "first,last,middle" + i).ToArray();  
        List<string> newNames = new List<string>(count);  
    
        Stopwatch stopWatch = new Stopwatch(); 
        stopWatch.Start();  
        foreach (string name in names)  
        {
            /* This approach only works for 2 commas */
            int firstComma = name.IndexOf(',');
            int lastComma = name.LastIndexOf(',');
    
            string first = name.Substring(0, firstComma);
            string last = name.Substring(firstComma + 1, lastComma-(firstComma+1));
            string middle = name.Substring(lastComma + 1);
    
            StringBuilder sb = new StringBuilder();  
            sb.Append(first).Append(middle).Append(last);  
    
            newNames.Add(sb.ToString());  
        } 
        stopWatch.Stop(); 
        stopWatch.Elapsed.Dump("IndexOf Time");  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This can certainly be done using a simple script and reading the database. I
My problem is certainly right on my face but I can't see it... I
I'm trying to create a Textmate snippet, but have run into some difficulties. Basically,
My question is similar to this one but I'm having some problems with the
in websql we can request a certain row like this: tx.executeSql('SELECT * FROM tblSettings
How can I escape certain characters in a string with a C# Regex? This
I have an array of values, I was wondering how I can parse certain
Can I have certain settings that are universal for all my users?
I am trying to run some JQuery script on a particular page but only
I have some simple XML which I would like to parse into an array

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.