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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:27:19+00:00 2026-06-10T06:27:19+00:00

I’m working with huge string data for a project in C#. I’m confused about

  • 0

I’m working with huge string data for a project in C#. I’m confused about which approach should I use to manipulate my string data.

First Approach:

StringBuilder myString = new StringBuilder().Append(' ', 1024);

while(someString[++counter] != someChar)
    myString[i++] += someString[counter];

Second Approach:

String myString = new String();

int i = counter;
while(soumeString[++counter] != someChar);
myString = someString.SubString(i, counter - i);

Which one of the two would be more fast(and efficient)? Considering the strings I’m working with are huge.

The strings are already in the RAM.
The size of the string can vary from 32MB-1GB.

  • 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-10T06:27:21+00:00Added an answer on June 10, 2026 at 6:27 am

    Per request from OP, here are my test results.

    Assumptions:

    • Big string is already in memory, no requirement for reading from disk
    • Goal is to not use any native pointers/unsafe blocks
    • The “checking” process is simple enough that something like Regex is not needed. For now simplifying to a single char comparison. The below code can easily be modified to consider multiple chars at once, this should have no effect on the relative performance of the two approaches.

      public static void Main()
      {
          string bigStr = GenString(100 * 1024 * 1024);
      
          Stopwatch sw = Stopwatch.StartNew();
          for (int i = 0; i < 10; i++)
          {
              int counter = -1;
              StringBuilder sb = new StringBuilder();
              while (bigStr[++counter] != 'x')
                  sb.Append(bigStr[counter]);
              Console.WriteLine(sb.ToString().Length);
          }
          sw.Stop();
          Console.WriteLine("StringBuilder: {0}", sw.Elapsed.TotalSeconds);
      
          sw = Stopwatch.StartNew();
          for (int i = 0; i < 10; i++)
          {
              int counter = -1;
              while (bigStr[++counter] != 'x') ;
      
              Console.WriteLine(bigStr.Substring(0, counter).Length);
          }
          sw.Stop();
          Console.WriteLine("Substring: {0}", sw.Elapsed.TotalSeconds);
      }
      
      public static string GenString(int size)
      {
          StringBuilder sb = new StringBuilder(size);
          for (int i = 0; i < size - 1; i++)
          {
              sb.Append('a');
          }
          sb.Append('x');
          return sb.ToString();            
      }
      

    Results (release build, .NET 4):

    StringBuilder ~7.9 sec

    Substring ~1.9 sec

    StringBuilder was consistently > 3x slower, with a variety of different sized strings.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me 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.