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 7565527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:40+00:00 2026-05-30T14:10:40+00:00

Im still learning in C#, and there is one thing i cant really seem

  • 0

Im still learning in C#, and there is one thing i cant really seem to find the answer to.

If i have a string that looks like this “abcdefg012345”, and i want to make it look like “ab-cde-fg-012345”

i tought of something like this:

  string S1 = "abcdefg012345";
            string S2 = S1.Insert(2, "-");
            string S3 = S2.Insert(6, "-");
            string S4 = S3.Insert.....
                ...
                ..

Now i was looking if it would be possible to get this al into 1 line somehow, without having to make all those strings.

I assume this would be possible somehow ?

  • 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-30T14:10:42+00:00Added an answer on May 30, 2026 at 2:10 pm

    Whether or not you can make this a one-liner (you can), it will always cause multiple strings to be created, due to the immutability of the String in .NET

    If you want to do this somewhat efficiently, without creating multiple strings, you could use a StringBuilder. An extension method could also be useful to make it easier to use.

    public static class StringExtensions
    {
        public static string MultiInsert(this string str, string insertChar, params int[] positions)
        {
            StringBuilder sb = new StringBuilder(str.Length + (positions.Length*insertChar.Length));
            var posLookup = new HashSet<int>(positions);
            for(int i=0;i<str.Length;i++)
            {
                sb.Append(str[i]);
                if(posLookup.Contains(i))
                    sb.Append(insertChar);
    
            }
            return sb.ToString();
    
        }
    }
    

    Note that this example initialises StringBuilder to the correct length up-front, therefore avoiding the need to grow the StringBuilder.

    Usage: "abcdefg012345".MultiInsert("-",2,5); // yields "abc-def-g012345"

    Live example: http://rextester.com/EZPQ89741

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still learning Scala, but one thing I thought was interesting is that Scala
I'm still learning Grails and seem to have hit a stumbling block. Here are
One thing that I like to do from time to time is do a
I am still learning C++, and I have never really created my own namespaces
I'm still learning ASP.NET and I often see code like this throughout parts of
I am learning to be a dba, and the one thing i am missing
So I am a relatively new programmer and am still learning, and have run
I'm trying to get to grips with Entity Framework and there is one thing
I'm new to Python and still learning. I was wondering if there was a
I'm still learning LINQ. I can do some simple queries, but this one is

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.