By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.