如何替换文本文件中的特定lat和long值?

时间:2017-02-17 06:48:03

标签: c# text-processing

string line = string.Empty;
int line_number = 1;
int line_to_edit = 2;

using (StreamReader reader = new StreamReader(@"C:\ut.txt"))
{
    using (StreamWriter writer = new StreamWriter(@"C:\ut1.txt"))
    {
        while ((line = reader.ReadLine()) != null)
        {
            if (line_number == line_to_edit)
            {
                writer.WriteLine(line);
            } 
            line_number++;
        }
    }
}

这是输入文件ut.txt

1
2  ----no. of splines   
6 0  --- 6 denotes no of lines in first spline
365608.901276044 1288380.47235694  ----1st line
365771.386298595 1288324.72422065
366114.128536966 1288263.42618414
366540.124442843 1288196.19420711
367310.251273576 1288150.21544542
368104.085736344 1288201.50959757----6th line
4 0  ----4 denotes no of lines in second spline      
368825.120730879 1301890.60510298----6th line in first spline replace this line  
368712.400375608 1301704.1672357
368741.48892057 1301615.81711783
368740.370207785 1301488.64428575----1st line in first spline replace this line

所需的输出是:

1
2
6 0
365608.901276044 1288380.47235694
365771.386298595 1288324.72422065
366114.128536966 1288263.42618414
366540.124442843 1288196.19420711
367310.251273576 1288150.21544542
368104.085736344 1288201.50959757
4 0
368104.085736344 1288201.50959757
368712.400375608 1301704.1672357
368741.48892057 1301615.81711783
365608.901276044 1288380.47235694

1 个答案:

答案 0 :(得分:0)

使用以下

更新您的代码
while ((line = reader.ReadLine()) != null)
{
  if (line.Contains("Specific Lat/Long Value"))
    {
        line.Replace("Specific Lat/Long Value", "New Value");
    }
}

上面的代码,用更新的值替换字符串。