Solved

Case Insensitive String Replace

Posted on 2004-03-25
7
2,789 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
Industry Leaders: 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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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