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
413 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now