在文件中替换字符串 [英] String replace in a file

查看:70
本文介绍了在文件中替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#创建一个应用程序来替换文件中的字符串.
应用程序的输入是我要替换的文件以及包含实际行及其替换行的xml文件.我正在读取xml文件,如果我的文件包含xml中存在的行,它将被替换. ;
我要替换字符串的第一次出现.

I am creating an application in c# for replacing a string in a file.
The input to the application are the file that I want to replace and a xml file that contains the actual line and its replacement line.I am reading the xml file and if my file contains the line which is present in the xml,it gets replaced;
I am replacing the first occurrence of the string.

private void Replace_Click(object sender, EventArgs e)
        {
            string dir = @"D:\Output";
            string path = XmltextBox.Text;
            string txtpath = Filetextbox.Text;
           
            if (!Directory.Exists(dir))  // if it doesn''t exist, create a directory
                Directory.CreateDirectory(dir);
            string file = Path.Combine(dir, "output.txt");
          
            System.Xml.XmlDocument docxml = new System.Xml.XmlDocument();
            XmlNodeList xmlnode;
            int i = 0;

            string str = null;
           
           
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            docxml.Load(fs);
            xmlnode = docxml.GetElementsByTagName("rule");
            for (i = 0; i <= xmlnode.Count - 1; i++)
            {
                 if (i == 1)//Background color change
                {
                    string line;
                    FileStream tempFile = new FileStream(despath, FileMode.Open);
                    StreamReader Templine = new StreamReader(tempFile16);

                    while ((line = Templine.ReadLine()) != null)
                    {
                        string linevalue = xmlnode[1].ChildNodes.Item(1).InnerText.Trim();

                        if (line.Contains(linevalue))
                        {
                          
                            string actual=line;
                                    
                            string replace16 ="currentClip.stopDrag();"

                            string replaceVariable = ReplaceFirstOccurence(text,actual, replace);

                            FileStream filewrite = new FileStream(despath, FileMode.Create);
                            StreamWriter filew = new StreamWriter(filewrite);
                            
                            filew.Write(replaceVariable);

                            filew.Close();
                            filewrite.Close();

                        }
                    }
                    Templine.Close();
                    tempFile.Close();
                }
               }
}


例如:我要替换"stopDrag();"行与"currentClip.stopDrag();"
如果我的文件包含"stopDrag();"只有一次,它就可以正常工作.但是,如果我的文件包含"stopDrag();",两次,然后将其替换为"currentClip.currentClip.stopDrag();"
如果出现3次,那么它将被替换3次.
如何避免这种替换?


For Eg: I want to replace the line "stopDrag();" with "currentClip.stopDrag();"
If my file contains "stopDrag();" only once then it is working fine.But if my file contains "stopDrag();" twice then it is getting replaced as "currentClip.currentClip.stopDrag();"
If there are 3 occurrences then it is getting replaced 3 times.
How to avoid such replacements?

推荐答案

试试这个
static public void ReplaceInFile( string filePath, string searchText, string replaceText )
{
    StreamReader reader = new StreamReader( filePath );
    string content = reader.ReadToEnd();
    reader.Close();

    content = Regex.Replace( content, searchText, replaceText );

    StreamWriter writer = new StreamWriter( filePath );
    writer.Write( content );
    writer.Close();
}


D4dilip.wordpress.com


D4dilip.wordpress.com


这篇关于在文件中替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆