473,396 Members | 1,975 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,396 software developers and data experts.

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 1937
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
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
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
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
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
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
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
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
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
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
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
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
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.