本文介绍了找到&替换C#中的确切单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试阅读一个txt文件,找到确切的单词并替换它。



Txt文件包含此文本:这是RezaFx中的Reza。

我正在尝试找到Reza并替换它而不是RezaFx中的Reza。

我使用的是基本代码下面;



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;

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:

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:

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,

推荐答案

string s = Regex.Replace(myInputString, @"\bReza\b", " Behnaz ", RegexOptions.IgnoreCase);


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>


这篇关于找到&amp;替换C#中的确切单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 15:24