Case-Insensitive Replace

  • Thread starter Thread starter Raterus
  • Start date Start date
R

Raterus

Howdy,

I'm making a search engine for an application I'm working on. I want to "bold" keywords they have searched for. I have a long string of words that represents a particular document, and I need to find out how to do a replace on this string, but have it be case-insensitive.
For instance:

dim docWords as string = "BLAH blah BlaH!"
dim myString as string = "blah"
docWords = docWords.Replace(myString, "<b>" & myString & "</b>")

My end result would be:
"<b>BLAH</b> <b>blah</b> <b>BlaH</b>!"

Right now, naturally, I only get this
"BLAH <b>blah</b> BlaH!"

I think may have to use Regex.Replace somehow, but I'm unsure how to proceed with that in order to make the search expression case insensitive. I really don't want to mess with the myString parameter too much as these words can be ever changing

I've tried using .tolower functions, which works great, but when it comes to displaying the result, everything is lowercase.

Any help would be great!!
--Michael
 
Try this

dim docWords as string = "BLAH blah BlaH!
dim myString as string = "blah
docWords = Replace(docWords, myString, "<b>" & myString & "</b>", 1, ,CompareMethod.Text

Hope this help
 
Back
Top