Solved

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

Posted on 2010-08-13
6
442 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:Bakersville
  • 4
6 Comments
 
LVL 8

Accepted Solution

by:
Yiogi earned 250 total points
ID: 33427902
There are two ways to do what you want.

a) You can use a regular expression search and replace the words with the links but keep the original string instead of the word you want to type. Let me know if you need for info on this.
b) You could also simply search for the word " Adult " for example including the spaces so that you are sure it's what you want and not a container in another word. Save the index and lets say your message variable is msg then you'd do this:
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>"

Hope it sounds clear to you.
0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 250 total points
ID: 33427906
You can keep the case by using reqular expression.

Example:
Dim s As String = "this IS TEST"
Dim findStr As String = "test"
Dim replStr As String = "MoreTest"
Dim rx As New Regex(findStr, RegexOptions.IgnoreCase)
Response.Write(rx.Replace(s, replStr))
0
 
LVL 1

Author Comment

by:Bakersville
ID: 33427985
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
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 1

Author Comment

by:Bakersville
ID: 33428115
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
0
 
LVL 1

Author Comment

by:Bakersville
ID: 33428255
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
0
 
LVL 1

Author Closing Comment

by:Bakersville
ID: 33428267
Thank you again
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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