[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 8

Accepted Solution

by:
Yiogi earned 1000 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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