Link to home
Start Free TrialLog in
Avatar of Bakersville
BakersvilleFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Replace word in string but keep the case the same and complete words only in asp.net vb

Hello everyone.

I am currently working on a website that will create websites for my firm andi am stuck.

On the website, whenever a few certain words are mention, it is hyperlinked.  This is done by me hardcoding.

I know the basic replace(string,"myWord","<a href etc>") , however, this does a few things that i need to work round.

Firstly, If the string has a word i want to replace is Adult and the word in the string is  adult, it changes it to a capital A.

Also, taking the same word to replace, if the word in the string is Adultery it will replace the Adult, but i would not want it replaced.

So is there a way of keeping the case when replacing a string and also is there any way of making it match the whole word only..

I hope this makes sense.

Baker
ASKER CERTIFIED SOLUTION
Avatar of Yiogi
Yiogi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bakersville

ASKER

Thank you both for your help.

om: i tried your example, but it does replace the middle of a word, so added Yiogi spaces in and that got round the issue.

However, how would i put the original word into the hyperlink, ie

<a href="www.google.co.uk">original word</a>

Yiogi: i tried your

msg = msg.Substring(0, indexOfWord) + replacementStringPrefix + msg.Substring(indexOfWord, wordLength) + replacementStringSuffix + msg.Substring(indexOfWord + wordLength)
where your replacement prefix would be the "<a href='adsfsdf'>" and suffix would be "</a>"


But i have to say, i do not understand it at all, could you please help???

Thank you again for your quick reply.

Baker
Kept looking round and found this

       Dim myString As String = "BlaH bLaH BLAH 1blah1"
        Dim rx As New Regex(" (blah) ", RegexOptions.IgnoreCase)
        myString = rx.Replace(myString, " <b>$1</b> ")
        Response.Write(myString)

It got over the issue of keeping the text the same font, but still can't find anywhere apart from putting spaces in unless anyone knows of a way...

Baker
Hello again both,

Thank you for putting me on the right path, found the solution by an Classic ASP expression i found. so change om code to read

        Dim WordToReplace As String = "blah"
        Dim myString As String = "BlaH bLaH BLAH blblahah"
        Dim rx As New Regex("\b(" & WordToReplace & ")\b", RegexOptions.IgnoreCase)
        myString = rx.Replace(myString, "<b>$1</b>")
        Response.Write(myString)

Now i ca make these change DB driven so if they change their mind i don't have to change my code...

Baker
Thank you again