Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying read one txt file, find exact word and replace that.

Txt file is include this text: This is Reza in RezaFx.
I am trying find exactly "Reza" and replace that and not "Reza" in "RezaFx".
I am using basic code as below;

C#
string str = File.ReadAllText("C://reza.txt");
            str= str.Replace("Reza", "Behnaz");// want replace "Reza" with Behnaz"
            File.WriteAllText("C://reza1.txt", str);


This format not works.
Tried this:
C#
string str = File.ReadAllText("C://reza.txt");
            str= str.Replace(@"/Reza/", "Behnaz");// want replace "Reza" with Behnaz"
            File.WriteAllText("C://reza1.txt", str);

Not works.
Tried this:
C#
string str = File.ReadAllText("C://reza.txt");
            str= str.Replace(@"/bReza/b", "Behnaz");// want replace "Reza" with Behnaz"
            File.WriteAllText("C://reza1.txt", str);

Not works.

Is there any solution for this?
Regards,
Posted
Comments
[no name] 28-Aug-13 17:12pm    
The first code works exactly like it should for me. I don't know about the other code. I did not test it as that code does not make any sense.
BillWoodruff 29-Aug-13 0:53am    
I note that you wish to offset the name "Behnaz" with extra spaces, before, and after it; is that exactly what you intended ?

You could use either @"/bReza/b" or "//bReza//b" in C#.

And, you might want to be aware of some interesting aspects of using escape-b in RegEx, which represents a work-boundary: http://msdn.microsoft.com/en-us/library/h5181w5w.aspx#WordBoundary'

If your original text was:

~

Reza, Txt file is include this text: This is Reza in fxReza.
I am trying find exactly "Reza" and replace that and not "Reza" in "RezaFx".
I am using basic code as below;

reza the "Reza," and, also known as: 'Reza,' Reza~Reza, the Reza:Reza;Reza, or reza-reza.

~

The output of your RegEx (based on OriginalGriff's answer, as revised by you) would be:

~

Behnaz , Txt file is include this text: This is Behnaz in fxReza.
I am trying find exactly " Behnaz " and replace that and not " Behnaz " in "RezaFx".
I am using basic code as below;

Behnaz the " Behnaz ," and, also known as: ' Behnaz ,' Behnaz ~ Behnaz , the Behnaz : Behnaz ; Behnaz , or Behnaz - Behnaz .

~

That, may, or may not, be exactly what you want ?

Use a regex:
string s = Regex.Replace(myInputString, @"\bReza\b", " Behnaz ", RegexOptions.IgnoreCase);
 
Share this answer
 
v2
Comments
Rezame 28-Aug-13 17:31pm    
This. this Format works:
string s = Regex.Replace(str, @"\bReza\b", " Behnaz ", RegexOptions.IgnoreCase);
OriginalGriff 28-Aug-13 17:55pm    
I'm on a tablet, and code is a PITA to type! :laugh:
Rezame 28-Aug-13 18:00pm    
Thanks.
BillWoodruff 29-Aug-13 1:03am    
Never, in my darkest blood-sweat fever-filled dreams of the drool of Cthulhu, did I imagine the day would come when I could actually "improve" an answer from Thee. However, 'twas many a day, and night, I was sure you could improve any answer from this idle wretch, and I am remain as sure of that as the creaking of my bones :) But, here, Sir, the word, "improve," really is preposterously pretentious: insertion of a single-character should hardly warrant felicitation.
OriginalGriff 29-Aug-13 4:37am    
My dear Sir, thine addition of but a single character has such beneficial effect upon my own poor effort as to render mine superfluous, unnecessary and redundant! Mayhap, had the Original Poster proceeded without thy mellifluous and sublime improvement; he would have been moved into the territory of "syntax error" and great confusion would indeed abound! Without thy observation and assistance, one would be confounded; swept against the rocks; to my eternal shame: an error. A stain.

He that commends me to mine own content commends me to the thing I cannot get. I to the world am like a drop of water that in the ocean seeks another drop, who, falling there to find his fellow forth, unseen, inquisitive, confounds himself: an error, plain and simple.

I suspect that only self immolation will expunge the stain...Out, damned spot! Out, I say! One; two: why, then, ’tis time to do ’t. Hell is murky! Fie, my lord, fie! A coder, and afeard? What need we fear who knows it, when none can call our code to account?
XML
In your code insert @ proerly so that your code will work

<pre lang="cs">string text = File.ReadAllText(@&quot;c:\trial.log&quot;);
          text = text.Replace(&quot;ResultCode&quot;, &quot;HI New value &quot;);
          File.WriteAllText(@&quot;c:\trial.log&quot;, text);</pre>
 
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