Solved

Case Insensitive String Replace

Posted on 2004-03-25
7
2,795 Views
Last Modified: 2012-05-04
While working with C#, I've come to find that the Replace() method does not have an option to perform a case insensitive search. This seems a little weird, but then again I've found it best not to try and figure Microsoft out. Regardless, after that, I started messing with RegEx thinking that might solve the problem. While I'm sure it can, I have yet to figure out how.

Basically, I am cycling through a Hashtable, and want to search a string (sBuffer) with the Hashtable key (sFind). If found, it replaces it with the Hashtable value (sReplace).

Any suggestions?

0
Comment
Question by:Thora_Fan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:simion_tishler
ID: 10684393
You could convert both to upper case before using replace. Is this too simple an answer.

Or alternatively you could write your own search and replace algorithm. I know a good one if your interested.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 10684407
Regex saves the day again!  Does this do the trick, Thora_Fan?

  string ReplaceString(string sBuffer, string sFind, string sReplace)
  {
    string regexString = "";
    foreach(char c in sFind)
      regexString += "[" + c.ToString().ToLower() + c.ToString().ToUpper() + "]";
    return System.Text.RegularExpressions.Regex.Replace(sBuffer, regexString, sReplace);
  }
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10684409
Ooooh simion, you snuck that one just ahead of me!
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Expert Comment

by:simion_tishler
ID: 10684424
Yeah well I need some points to give people for helping me. So I thought I'd try and help with some answers.
0
 

Author Comment

by:Thora_Fan
ID: 10684723
Thank you both for answering. That did the trick indeed, eternal_21. Thanks a lot.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10724101
Hey Simion, came across this and thought I would pass it on to you:

  string ReplaceString(string sBuffer, string sFind, string sReplace)
  {
    return System.Text.RegularExpressions.Regex.Replace(sBuffer, sFind, sReplace, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  }

Nice option, eh?
0
 
LVL 1

Expert Comment

by:simion_tishler
ID: 10728071
Yeah well

after all that Microsoft wasn't as stupid as we thought.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question