test.txt文档内容如下,
test
test
test
123456
1234567
12345678
123456789
/test
/test
/test
......
txt文档内容格式如下,按行读取,最前面有多少test不确定,
最后面/test跟最前面的test行数是一样的。中间内容无所谓,
现在需求如下,删除文档中的test和/test,,保留一个即可
删除后内容如下。
test
123456
1234567
12345678
123456789
/test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Q691618
{
class Program
{
static void Main(string[] args)
{
string filename = "c:\\test.txt";
var lines = File.ReadAllLines(filename);
var query = lines.SkipWhile(x => x.Trim() == "test").TakeWhile(x => x.Trim() != "/test").ToArray();
File.WriteAllLines(filename, lines.Take(1).ToArray());
File.AppendAllLines(filename, query);
File.AppendAllLines(filename, lines.Skip(lines.Length - 1).ToArray());
}
}
}