Replace string

hi i have a prolblem. i want to search a text for a string and replace it with another string

i'm using String.Replace("oldval","newval");

but the problem for example i have text "in the morning i'll say. withen two days i'll go "
let's say i want to replace "the" with "WE" .when using string .replace .it results

"in WE morning i'll say,wiWEn two days i'll go" .it replace a part of the word. iwant it .to repalce a whole words .not the parts of other i clear> ihope i can find help
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hema3iAuthor Commented:
and also it may contains two words or three .or more than

i found Regex.Replace();

using the expression \bTheWord\b

what is the expression . for one two or three or more words
//***You can put spaces to both sides of the strings like this
i'm using String.Replace(" oldval "," newval ");
PeterFearnCommented: is a good place to look.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
As PeterFearn mentioned, regex is the best solution for your problem.

Regex class or Regular Expressions is a wonderful and very powerfull text  parsing  tool.

It has a lot of various functions but I'll show only what you need.
The main idea is to "create" a special pattern within your string that matches exactly what you need.

First we must inherit Regex class, we do so by:

using System.Text.RegularExpressions;

string test= "in the morning i'll say. withen two days  i'll go "; //your string
string pattern = @"\bthe\b"; //the pattern: \b       - Matches a word boundary, the position
                                         // between a word and a space. (from left and right)
//in other words, it finds "the" without leading and trailing letters. But if doesn't exclude non word //chars like - , . etc' (there is another pattern for that)

string test1 = Regex.Replace(test,pattern,"we");
//Use regex's replace to replace :)   (test=your string, patter=search pattern, "we" = new string)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.