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

  • Home
  • SEARCH
  • 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 1113463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:51:41+00:00 2026-05-17T02:51:41+00:00

string str = ‘my {0} long string {1} need formatting’; Should I be doing

  • 0
string str = 'my {0} long string {1} need formatting';

Should I be doing the following,

str = string.Format(str, "really", "doesn't");

or creating a method like so and calling str = str.ReplaceWithValues("really", "doesn't");

 public string ReplaceWithValues(this string str, params object[] values) {
    string ret = str;
    for (int i = 0; i < values.Length; i++) {
        ret = str.Replace(string.Concat("{", i, "}"), values.ToString());
    }
    return ret;
}

It seems like StringBuilder.AppendFormat() isn’t efficient when it comes to doing simple replacements like this since it goes character by character through the string.

  • 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-17T02:51:42+00:00Added an answer on May 17, 2026 at 2:51 am

    Why do you want to reinvent String.Format?

    I’d just use the framework method – it does exactly what you want, is robust, and is going to make sense to those that follow…


    Just to satisfy your curiousity:

    It seems like StringBuilder.AppendFormat() isn’t efficient when it comes to doing simple replacements like this since it goes character by character through the string.

    String.Format, FYI, uses StringBuilder.AppendFormat internally. That being said, StringBuilder.AppendFormat is quite efficient. You mention that it goes “character by character” through the string – however, in your case, you’re using multiple calls to Replace and Concat instead. A single pass through the string with a StringBuilder is likely to be much quicker. If you really need to know- you could profile this to check. On my machine, I get the following timings if I run both of the above 1,000,000 times:

    String.Format -  1029464 ticks
    Custom method -  2988568 ticks
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string with the following format: String str = someString(anotherString)(lastString); I wanted
The following code doesn't work: class String{ public: char* str; int* counter; String(){ str
I have the following string: str = MMX Lions Television Inc And I need
I have a string like the following: String str = 4*5; Now I have
In Java I have: String str = Welcome 'thanks' How are you?; I need
I have the following string : str = {application.root.category.id:2} I would like to convert
My query looks like: string str = string.Format(Insert into [MyDB].[dbo].[tlb1] ([file_path],[CONTENT1],[CONTENT2]) values ('{0}','{1}','{2}');, fullpath,
There is a string: str = first line\n xxxxxxxxxxxxx\n zzzzzzzzzzzzzzz I need to get
I have following string String str = replace :) :) with some other string;
string str = Student_123_; I need to replace the last character _ with ,.

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.