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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:53:25+00:00 2026-05-21T05:53:25+00:00

I recently had to perform some string replacements in .net and found myself developing

  • 0

I recently had to perform some string replacements in .net and found myself developing a regular expression replacement function for this purpose. After getting it to work I couldn’t help but think there must be a built in case insensitive replacement operation in .Net that I’m missing?

Surely when there are so many other string operations that support case insensitive comparission such as;

var compareStrings  = String.Compare("a", "b", blIgnoreCase);
var equalStrings    = String.Equals("a", "b", StringComparison.CurrentCultureIgnoreCase);

then there must be a built in equivalent for replace?

  • 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-21T05:53:26+00:00Added an answer on May 21, 2026 at 5:53 am

    Found one in the comments here: http://www.codeproject.com/Messages/1835929/this-one-is-even-faster-and-more-flexible-modified.aspx

    static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType)
    {
         return Replace(original, pattern, replacement, comparisonType, -1);
    }
    
    static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType, int stringBuilderInitialSize)
    {
         if (original == null)
         {
             return null;
         }
    
         if (String.IsNullOrEmpty(pattern))
         {
             return original;
         }
    
    
         int posCurrent = 0;
         int lenPattern = pattern.Length;
         int idxNext = original.IndexOf(pattern, comparisonType);
         StringBuilder result = new StringBuilder(stringBuilderInitialSize < 0 ? Math.Min(4096, original.Length) : stringBuilderInitialSize);
    
         while (idxNext >= 0)
         {
            result.Append(original, posCurrent, idxNext - posCurrent);
            result.Append(replacement);
    
            posCurrent = idxNext + lenPattern;
    
            idxNext = original.IndexOf(pattern, posCurrent, comparisonType);
          }
    
          result.Append(original, posCurrent, original.Length - posCurrent);
    
          return result.ToString();
    }
    

    Should be the fastest, but i haven’t checked.

    Otherwise you should do what Simon suggested and use the VisualBasic Replace function. This is what i often do because of its case-insensitive capabilities.

    string s = "SoftWare";
    s = Microsoft.VisualBasic.Strings.Replace(s, "software", "hardware", 1, -1, Constants.vbTextCompare);
    

    You have to add a reference to the Microsoft.VisualBasic dll.

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

Sidebar

Related Questions

Recently I had to type up some documentation on .net data providers and ado.net.
I recently had a problem in my app where some of the subviews I
I recently had to take a quick look at Adobe InDesign server. In this
I recently had some problems with my VS 2008, and was recommended to reinstall.
I recently had cause to work with some Visual Studio C++ projects with the
I recently had an issue after upgrading my .net framework to 4.0 from 3.5:
I've recently had to put some code into a different server and my .htaccess
I recently had a need to interpret a DEC 32-bit floating point representation. It
I recently had to work on a project where the previous developer modified the
We recently had a code review . One of my classes was used so

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.