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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:06:50+00:00 2026-05-24T11:06:50+00:00

To build a sparsely populated fixed width record, I would like to copy a

  • 0

To build a sparsely populated fixed width record, I would like to copy a string field into a StringBuilder object, starting at a given position. A nice syntax for this would have been

StringBuilder sb = new StringBuilder(' ', 100);
string fieldValue = "12345";
int startPos = 16; 
int endPos = startPos + fieldValue.Length - 1;
sb[startPos..endPos] = fieldValue; // no such syntax

I could obviously do this C style, one character at a time:

for (int ii = 0; ii++; ii < fieldValue.Length)
    sb[startPos + ii] = fieldValue[ii];

But this seems way too cumbersome for c#, plus it uses a loop where the resulting machine code could more efficiently use a bulk copy, which can make a difference if the strings involved were long. Any ideas for a better way?

  • 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-24T11:06:51+00:00Added an answer on May 24, 2026 at 11:06 am

    You can write your own specialized string builder class that uses the efficient machinery of char[] and string underneath the hood, in particular String.CopyTo:

    public class FixedStringBuilder
    {
        char[] buffer;
    
        public FixedStringBuilder(int length)
        {
            buffer = new string(' ', length).ToCharArray();
        }
    
        public FixedStringBuilder Replace(int index, string value)
        {
            value.CopyTo(0, buffer, index, value.Length);
            return this;
        }
    
        public override string ToString()
        {
            return new string(buffer);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            FixedStringBuilder sb = new FixedStringBuilder(100);
            string fieldValue = "12345";
            int startPos = 16; 
            sb.Replace(startPos, fieldValue);
            string buffer = sb.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For build automation, I would like to invoke the XML Data Binding Wizard in
Our build has a variety of generated HTML reports. I would like to have
I build up an array of strings with string[] parts = string.spilt( ); And
I build dynamically my HTML table from database like that: for (i = 0;
My build agent working directories are starting to take too much space on the
We build our cppunit unittests as a dll and load it into TestPlugInRunnerd.exe to
my build.gradle looks like: apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'application' ......
I build applications using C++, C# and Java. But when I try to copy
I build websites for a small-ish media company. Unfortunately, around 45-50% of our client
My build process consists of Qt's qmake Makefile generator and the typical make utility

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.