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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:39:22+00:00 2026-05-29T21:39:22+00:00

I just wondered what’s the easiest way to replace a string characters that must

  • 0

I just wondered what’s the easiest way to replace a string characters that must be replaced subsequently.

For example:

var str = "[Hello World]";
//enclose all occurences of [ and ] with brackets[] 
str = str.Replace("[","[[]").Replace("]","[]]");
  • The desired result: [[]Hello World[]]
  • The actual result: [[[]]Hello World[]]

The reason is obviously the second replace on the already modified string.

So how to replace all occurences of “bad” characters with characters that contain “bad” characters?


A quick measurement of all approaches revealed that the StringBuilder is the most efficient way.

190kb file (all in milliseconds)

  regexTime           40.5065  
  replaceTime         20.8891  
  stringBuilderTime    6.9776

7MB file

  regexTime           1209.3529           
  replaceTime          403.3985   
  stringBuilderTime    175.2583

By the way, the direct StringBuilder approach from John was twice as fast as the Aggregate approach from Sehe.

I’ve made an extension out of it:

public static String EncloseChars(this string input, char[] charsToEnclose, String leftSide, String rightSide) {
    if (charsToEnclose == null || leftSide == null || rightSide == null)
        throw new ArgumentException("Invalid arguments for EncloseChars", charsToEnclose == null ? "charsToEnclose" : leftSide == null ? "leftSide" : "rightSide");
    Array.Sort(charsToEnclose);
    StringBuilder sb = new StringBuilder();
    foreach (char c in input) {
        if (Array.BinarySearch(charsToEnclose, c) > -1)
            sb.Append(leftSide).Append(c).Append(rightSide);
        else 
            sb.Append(c);
    }
    return sb.ToString();
}

"[Hello World]".EncloseChars(new char[]{'[', ']'},"[","]");
  • 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-29T21:39:25+00:00Added an answer on May 29, 2026 at 9:39 pm

    Here’s a very uncool way to do it. But it has the advantage of being pretty close to foolproof, I think, and not using regex (in case you’d rather not use regex).

    StringBuilder sb = new StringBuilder();
    foreach (char c in str.ToCharArray()) {
        if (c == '[' || c == ']') {
            sb.Append('[' + c + ']');
        }
        else {
            sb.Append(c);
        }
    }
    string result = sb.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working selector but I just wondered whether there was another way
I just wondered if anyone knew of a simple script available that will do
I just wondered how people were approaching this situation. It's something that seems like
I just wondered if there were an native PHP function that would replicate the
I just wondered if this is in some way a css attribute: http://dl.dropbox.com/u/14645664/fhjfhgf.JPG Does
Just wondered if anyone knew a way to animate leaves falling in the wind?
Just wondered if anyone could give me some advice on the best way to
I just wondered what the best way is to get rid of the globally
Just wondered if any LINQ guru might be able to shed light on how
I just wondered, why most Delphi examples use FillChar() to initialize records. type TFoo

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.