473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

Avi
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi
Jun 27 '08 #1
8 2265
Avi wrote:
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?
In a regular expression pattern you can use \b to indicate a word
boundary so that this code

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);

outputs

baz foobar barfoo baz
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write(myNewText)

"Avi" <re*********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like
it to replace only full words that matches oldValue and not when oldValue
is a substring of a larger word.

How can it be done?

Thanks,
Avi
Jun 27 '08 #3
ThatsIT.net.au wrote:
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write(myNewText)
That doesn't make it any better.

Given this replacement:

Dim oldWord = "all"
Dim newWord = "none"

you would get the string "once upon a time in a smnone town "

Also notice the extra space added at the end...
"Avi" <re*********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi all,

I'm using string Replace(string oldValue, string newValue) and would
like it to replace only full words that matches oldValue and not when
oldValue is a substring of a larger word.

How can it be done?

Thanks,
Avi

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4
Avi wrote:
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi
As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(oldValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(text, pattern, newValue);

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #5
On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.comwrote:
Avi wrote:
Hi all,
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.
How can it be done?
Thanks,
Avi

As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(oldValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(text, pattern, newValue);

--
Göran Andersson
_____http://www.guffa.com


Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);
Works out nicely.
-Cnu
Jun 27 '08 #6
Duggi wrote:
On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.comwrote:
>Avi wrote:
>>Hi all,
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.
How can it be done?
Thanks,
Avi
As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(oldValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(text, pattern, newValue);

--
Göran Andersson
_____http://www.guffa.com

Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(example, @"\bfoo\b", "baz");
Console.WriteLine(result);
Works out nicely.
-Cnu
Yes, that works if you have full control over the input, so that you can
be sure that it doesn't contain any characters that have a special
meaning in a regular expression.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #7
Avi
Thank you all for your help!

Avi
"Avi" <re*********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like
it to replace only full words that matches oldValue and not when oldValue
is a substring of a larger word.

How can it be done?

Thanks,
Avi

Jun 27 '08 #8
Yes your right, didn't think it though, A regex is the way to go.
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
ThatsIT.net.au wrote:
> Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write(myNewText)

That doesn't make it any better.

Given this replacement:

Dim oldWord = "all"
Dim newWord = "none"

you would get the string "once upon a time in a smnone town "

Also notice the extra space added at the end...
>"Avi" <re*********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Hi all,

I'm using string Replace(string oldValue, string newValue) and would
like it to replace only full words that matches oldValue and not when
oldValue is a substring of a larger word.

How can it be done?

Thanks,
Avi


--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
2
by: Raed Sawalha | last post by:
I have the following text:- Brian went to stadium to watch the soccer game, Brian MacWoods is bussiness man and very rich man. Brian likes to run every morning on beachside. the problem i...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
8
by: shapper | last post by:
Hello, I have a string which holds a text. Is it possible to create a substring which uses the first N words of that string? Thanks, Miguel
0
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how...
10
by: Robert R. | last post by:
Hello, i would like to write a piece of code to help me to align some sequence of words and suggest me the ordered common subwords of them s0 = "this is an example of a thing i would like to...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.