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

The Archive Base Latest Questions

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

What is faster having a String and then calling Substring for this String where

  • 0

What is faster having a String and then calling Substring for this String where the first Substring will be the initial String and every new Substring a smaller portion from within the initial substring until its end? Or using a StringBuilder and cutting off more and more from the beginning and then always using ToString from the shrinking string?

This link describes that the StringBuilder ToString method only allocates new space if a new thread accesses the StringBuilder or the returned String is much smaller than the currently allocated space. This sounds faster than using Substring. I can see this logic within the implementation up to .Net Framework 3.5. With version 4.0 there is an immediate call to FastAllocateString (more or less like with String Substring). Is it the same logic as in previous framework versions, only hidden now?

Why I need it:

Edit: Updated explanation:
I have a string and a set of user given regexes and a context. The context tells me which regexes would be currently interesting to try to match with the start of the string. After a successful match the start of the string is now after where the last match ended. And now there may be a different context. And additionally I want to know which Regex matched with the start the last time.
Since this is done multiple times the performance should be as good as possible.

In order to avoid searching the whole (remaining) string, I would use the start-anchor (^), so the search can be abborted as soon as possible (after knowing that the start does not match). If I do this I have the problem that I cannot use the start index parameter of the Match method with anything other than 0. Otherwise there would never be a match thanks to the anchor.

So I think I have two possibilities now:

1) Always using the Substring method on the input string and keeping and index for the current position.

2) Using a StringBuilder and always removing the start of the input when matched successfuly. Then calling the ToString method to see if the new start matches with the next Regex.

Is the performance of one or both of the above approaches acceptable, or is there another way which is faster?

Edit2: Regarding StringBuilder and String: As described here the implementation of StringBuilder now immediately allocates the space whereas previously this was delayed until the StringBuilder string was changed (which would have been the case all the time for my requirements). So I guess both Substring and ToString are very slow compared to the Regex-only solution from Kendall Frey.

  • 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-08T13:25:51+00:00Added an answer on June 8, 2026 at 1:25 pm

    You don’t need to do this. The .NET regex engine includes a feature that lets you match a string immediately following the previous match, using the \G anchor.

    Example regex:

    \Gcat
    

    Example string:

    catcatcatdogcat
    

    Matches:

    (cat)(cat)(cat)dogcat
    

    Edit: I don’t think the regex lets you remember the \G anchor position between multiple regexes. Instead, you can use Regex.Match(string, int) overload to begin your string at a certain point, and use the \G anchor to match the starting position.

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

Sidebar

Related Questions

Is it better / faster inside an event listener to use this or event.target
which one is faster select * from parents p inner join children c on
Easy question this time. I'm trying to test whether or not a string does
Two way of doing things and I'm curious which is faster: First way: if
I have a C++ program that will read in data from a binary file
I was wandering what keep MongoDB faster. Having a few parent documents with big
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
Is it generally better/faster to do: if (condition) return a else if (condition2) return
which is better or faster, A or B? std::deque<Myclass> queue; ... // do something
Which one is faster? Which one uses less memory? Console.WriteLine(string1) Console.WriteLine(string2) Console.WriteLine(string3) Console.WriteLine(stringNth) or

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.