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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:04:49+00:00 2026-06-08T23:04:49+00:00

Given a sting, how can I skip the first x characters and then insert

  • 0

Given a sting, how can I skip the first x characters and then insert a value for every y characters?

For example:

“Lorem ipsum dolor sit amet,” 

when skipping the first 10 caracters and then indsert “[here]” for every 3 caracters becomes:

“Lorem ipsu[here]m d[here]olo[here]r s[here]it [here]ame[here]t,”

What is the most efficient, fastest way of doing this in C#?

My current function looks like this but isn’t doing the skipping part, I know how to implement the skipping part but the technique used does not seem to be optimal:

public static string InsertHere(string source)
    {
        if (string.IsNullOrWhiteSpace(source))
        {
            return string.Empty;
        }

        int count = 0;
        var sb = new StringBuilder();
        foreach (char c in source)
        {
            count++;
            sb.Append(c);
            if (count == 10)
            {
                count = 0;
                sb.Append(@"[here]");
            }
        }
        return sb.ToString();
    }
  • 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-06-08T23:04:51+00:00Added an answer on June 8, 2026 at 11:04 pm

    You’d have to profile them to see what’s best, but here’s my effort, I’ve gone for a string reader into a buffer approach.

        public static string InsertStringRepeatedly(string source, int skip, int insertEvery, string toInsert)
        {
            var sb = new StringBuilder();
            using (var sr = new StringReader(source))
            {
                var buffer = new char[Math.Max(skip, insertEvery)];
                var read = sr.Read(buffer, 0, skip);
                sb.Append(buffer, 0, read);
                while (sr.Peek() > 0)
                {
                    sb.Append(toInsert);
                    read = sr.Read(buffer, 0, insertEvery);
                    sb.Append(buffer, 0, read);
                }
            }
            return sb.ToString();
        }
    

    Edit:

    Fixed for edge cases source.Length < 10 or not whole multiple of 10 + 3*x. Also use just one buffer now.

    Usage example:

        private static void Main(string[] args)
        {
            var result = InsertStringRepeatedly("Lorem ipsum dolor sit amet,", 10, 3, "[Here]");
            Console.Write("\"");
            Console.Write(result);
            Console.WriteLine("\""); //quotes to show we dealt with edge cases correctly
            Console.ReadLine();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a string such as: 23,234,456 first second third How can I split string
How can I add double quote characters to a given string? local str =
In most languages like C# for example given a string you can test (boolean)
What regular expression can I use (if any) to validate that a given string
Given a string: hellothis is a testthis is another testetc How can I write
Given a string such as: new/path - path/path/03 - filename.ext, how can I use
Given the following: String s = The The The the the the; How can
I have a string.An example is given below. [playlist]\r\npath1=url1\r\npath2=url2\r\npath=url3\r\npath4=url4\r\ncount=1 How can I extract path
Can anybody give me a little advice please? I have a string, for example
I need ur help for given subject. I am playing with htaccess rules first

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.