473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace whole word

If i use the replace function in c#

string a = "this";
a.replace("is", "something");

my output would be like thsomething
how can i replace when only the whole word matches.

string a = "is";
a.replace("is", "something");

output: something

Thanks
Aaron
Jul 21 '05 #1
6 1939
Use:

string a = "is";
a = Regex.Replace(a, @"\bis\b", "something");

Niki

"Aaron" <ku*****@yahoo.com> wrote in
news:Oz*************@TK2MSFTNGP10.phx.gbl...
If i use the replace function in c#

string a = "this";
a.replace("is", "something");

my output would be like thsomething
how can i replace when only the whole word matches.

string a = "is";
a.replace("is", "something");

output: something

Thanks
Aaron

Jul 21 '05 #2
what does \b mean? backspace?

what if the string is just one work "is" not " is "??

"Niki Estner" <ni*********@cube.net> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
Use:

string a = "is";
a = Regex.Replace(a, @"\bis\b", "something");

Niki

"Aaron" <ku*****@yahoo.com> wrote in
news:Oz*************@TK2MSFTNGP10.phx.gbl...
If i use the replace function in c#

string a = "this";
a.replace("is", "something");

my output would be like thsomething
how can i replace when only the whole word matches.

string a = "is";
a.replace("is", "something");

output: something

Thanks
Aaron


Jul 21 '05 #3
"Aaron" <ku*****@yahoo.com> wrote in
news:eZ**************@TK2MSFTNGP09.phx.gbl...
what does \b mean? backspace?

"\b" matches for a word boundary. See: http://tinyurl.com/5uvwz
what if the string is just one work "is" not " is "??

Not sure what you mean... "\b is \b" (including the spaces) would match:
"this is fine", but not "a, is b", because ", " is no word boundary.

Niki

Jul 21 '05 #4
what is the string only has one word
a = "is";

there's no /b in that string
"Niki Estner" <ni*********@cube.net> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
"Aaron" <ku*****@yahoo.com> wrote in
news:eZ**************@TK2MSFTNGP09.phx.gbl...
what does \b mean? backspace?

"\b" matches for a word boundary. See: http://tinyurl.com/5uvwz
what if the string is just one work "is" not " is "??

Not sure what you mean... "\b is \b" (including the spaces) would match:
"this is fine", but not "a, is b", because ", " is no word boundary.

Niki

Jul 21 '05 #5
Aaron wrote:
what is the string only has one word
a = "is";

there's no /b in that string


Sure there is. End of string is a word boundary. Why would you think
it isn't? Remember in a regular expression, there is not necessarily a
one-to-one correlation between letter in the pattern and letters in the
matched string.

--
Truth,
James Curran [MVP]
www.NJTheater.com (Professional)
www.NovelTheory.com (Personal)


Jul 21 '05 #6
As explaind in the link I've sent you, "\b" is a zero-width assertion: It
doesn't match a character, it matches a position in a string where left of
the position is a word character (a-z,A-Z,0-9,_), while right of it isn't
(or vice versa). In the string "is", at position 0 there'a word character
('i') to the right, but no word character to the left, thus \b matches. Same
applies for the end of the string.

Niki

"Aaron" <ku*****@yahoo.com> wrote in
news:e3**************@TK2MSFTNGP09.phx.gbl...
what is the string only has one word
a = "is";

there's no /b in that string
"Niki Estner" <ni*********@cube.net> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
"Aaron" <ku*****@yahoo.com> wrote in
news:eZ**************@TK2MSFTNGP09.phx.gbl...
what does \b mean? backspace?

"\b" matches for a word boundary. See: http://tinyurl.com/5uvwz
what if the string is just one work "is" not " is "??

Not sure what you mean... "\b is \b" (including the spaces) would match:
"this is fine", but not "a, is b", because ", " is no word boundary.

Niki


Jul 21 '05 #7

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

Similar topics

3
17788
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
2
6444
by: Babu Mannaravalappil | last post by:
Hi, I want to replace some words in my text files (actually transpose). For example, I have a whole lot of expressions (words) in my files as follows: TABLECUSTOMERS TABLEORDERS...
2
14881
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
6
11730
by: Aaron | last post by:
If i use the replace function in c# string a = "this"; a.replace("is", "something"); my output would be like thsomething how can i replace when only the whole word matches. string a =...
18
1545
by: Jon S via DotNetMonster.com | last post by:
Hi all, I searching a string to see if "Desc" resides in it, if so then replace it with "Description". For some reason if it finds "Desc" in the word "Description" it replaces the "Desc" part...
1
5104
by: James Vitale | last post by:
Using vb asp.net 1.1 I'm doing a word automation on a doc file and trying to do a find and replace. My existing code works fine except that it doesn't find and replace in the header. My code...
0
3066
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
9025
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
2
1466
by: Giakko | last post by:
Dear readers, i need help on a regular expression: i have to apply 100 different replaces to an article, but i don't want that a portion of text already replaced is replaced another time by...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6905
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
7395
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
5485
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,...
1
4921
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...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.