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 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/
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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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)....
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |