在文件中查找替换文本 [英] Find Replace Text in File

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

问题描述





我要求在文本文件中搜索一个值,并将其替换为用户输入的值。该文件具有以下格式:

Hi,

I have a requirement to search for a value in a text file and replace it with a value entered by a user. The file is of the following format:

[Value1]
Key1=0
Key2=1
Key3=2

[Value2]
Key1=0
Key2=1
Key3=2

[Value3]
Key1=0
Key2=1
Key3=2



等..



我需要成为能够搜索值/密钥对并编辑与密钥关联的值。例如,我希望能够调用类似的函数:


etc..

I need to be able to do a search on a value/key pair and edit the value associated with the key. For example I'd like to be able to call a fucntion something like:

WriteParam("[Value1]","Key3","123")



这将生成


This will generate

[Value1]
Key1=0
Key2=1
Key3=123



到目前为止,我已经想出了这个:


So far I have come up with this:

private void WriteTest()
       {

           List<string> lines = new List<string>(File.ReadAllLines(@"D:\InitiatorVals.ini"));
           foreach(string line in lines)
           {
               if((string.Compare(line,"[Type1]"))==0)
               {
                  //Found the value how to locate the key and edit the associated value

               }
           }
       }



通常我会坚持不懈并弄明白对我自己,但我受时间限制。因此,我将非常感谢任何和所有的帮助。



非常感谢


Normally I would persevere and figure it out for myself but I'm under time constraints. Therefore I would be very grateful for any and all assistance.

Thanks so much

推荐答案

你好



见以下链接





使用C#的INI文件处理类 [ ^ ]


试试这个:

Try this:
private void WriteParam(string section, string key, string value)
{
    List<string> lines = new List<string>(File.ReadAllLines(@"D:\InitiatorVals.ini"));
    int index = lines.IndexOf(section);
    if (index > -1)
    {
        bool valueChanged = false;
        while (!lines[++index].StartsWith("["))
        {
            if (lines[index].StartsWith(String.Concat(key, "=")))
            {
                lines[index] = String.Format("{0}={1}", key, value);
                valueChanged = true;
                break;
            }
        }
        if (!valueChanged)
        {
            // key not found, create it if you want
        }
    }
    else
    {
        // section not found, if you want, create a new one
    }

    File.WriteAllLines(@"D:\InitiatorVals.ini", lines);
}



行[++ index] ,您需要添加<$变量名之前的c $ c> ++ ,因为这样可以确保操作的返回值是变量递增后的值。


At lines[++index], it is necessary that you add the ++ before the variable name, because that makes sure that the return value of the operation is the value after the variable is incremented.


这里完整代码是我的小逻辑。希望它可以给你一些更多的想法。



Here is my little logic with out the full code. hope it may give you some more idea.

<pre lang="c#">
private void WriteTest()
{

List lines = new List(File.ReadAllLines(@"D:\InitiatorVals.ini"));
foreach(string line in lines)
{
if((string.Compare(line,"[Type1]"))==0)
{
//Here is the new changes
//store the position to say int x
//find next index of '['
//store it to y

//from x to y in loop 
//find the key with equal sign ex: 'Key2='
//store the index and count upto new line character 
//replace it
//

}
}
}


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

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