Click here to Skip to main content
15,792,397 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to replace the whole word and not partial matches.
for eg:I want to replace ".addListener" string in a file.But I don't want to replace string that contains "key.addListener".
Is there a way to do such replacements?
MT_ 12-Oct-12 2:31am    
with regex you can definitely replace the whole word, chk solution below.

if you are asking for doing it in C# (code)file not for run-time code then in .net not such facility.
you can use some trick,
first replace key.addListener with some string suppose with your name
now, replace .addListener with whatever you want.
and then re-replace your name with key.addListener

Happy Coding!
Share this answer
You may want to use Regex , a sample code below may help

string inputString = "key.addListener .addListener"; 
string patternToReplace= @"\B.addListener\b"; 
string result = Regex.Replace(inputString , patternToReplace, ""); 

Now result will contain string for which only whole word .addListener is removed

Note: Since the start of hte word you are looking to remove is . the first part of the regex is /B If it is normal word like addListener without the . then use /b at the start of the regex pattern.

Hope that helps.
Do mark as answer/upvote if it helps
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900