Solved

Case Insensitive String Replace

Posted on 2004-03-25
7
2,772 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now