473,386 Members | 1,790 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,386 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
Nov 16 '05 #1
6 11728
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

Nov 16 '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


Nov 16 '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

Nov 16 '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

Nov 16 '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)


Nov 16 '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


Nov 16 '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...
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 =...
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...
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...
2
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.