Solved

Case Insensitive String Replace

Posted on 2004-03-25
7
2,780 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 51
How can my static class become undefined?? 8 47
Getting error in connectionstring with Excel. 30 31
FTP file download using c# 3 22
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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