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
436 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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