473,624 Members | 2,160 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 1950
Use:

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

Niki

"Aaron" <ku*****@yahoo. com> wrote in
news:Oz******** *****@TK2MSFTNG P10.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*********@cu be.net> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
Use:

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

Niki

"Aaron" <ku*****@yahoo. com> wrote in
news:Oz******** *****@TK2MSFTNG P10.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******** ******@TK2MSFTN GP09.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*********@cu be.net> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
"Aaron" <ku*****@yahoo. com> wrote in
news:eZ******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP09.phx.gbl...
what is the string only has one word
a = "is";

there's no /b in that string
"Niki Estner" <ni*********@cu be.net> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
"Aaron" <ku*****@yahoo. com> wrote in
news:eZ******** ******@TK2MSFTN GP09.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
17808
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 ie. "hello world" will be come... "_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like one line!) with the method i have written only the last char is replaced, loop problem i guess! public String hideWord(String word) {
2
6464
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 TABLEORDERLINES ....
2
14904
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 in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
6
11741
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 = "is"; a.replace("is", "something");
18
1561
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 of it with "Description thus having the final outcome of "Descriptionription". Is there a way to have the replace() method only replace if the entire string is "Desc" only and not "Desc* " ? Hope this makes sense. Thanks in advance. if(...
1
5113
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 to set the selection is as follows: 'This sets the selection to be the whole document
0
3074
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 want to replace all of them. You need to look at it in a case-by-case basis. What can you do? Answer: emacs.
1
9051
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 string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100
2
1475
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 another text. the php function strtr() does this but for other reasons i have to use ereg_replace or preg_replace to make you understand what i need i paste a portion of the strtr() function documentation:...
0
8236
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8621
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8475
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4079
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
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 we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.