更新文本文件C#中的特定行

时间:2017-04-17 12:02:36

标签: c# asp.net .net

我尝试使用以下条件更新文本文件中的特定行: 如果行包含 Word-to-search ,则仅删除下一个空格

使用吹气代码:

using (System.IO.TextReader tr = File.OpenText((@"d:\\My File3.log")))
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {

                    string[] items = line.Trim().Split(' ');

                    foreach (var s in items)
                    {

                        if (s == "a" || s == "b")


                            s = s.Replace(" ", "");
                        using (StreamWriter tw = new StreamWriter(@"d:\\My File3.log"))  

                        tw.WriteLine(s);

我的文件是llike:

k l m

x y z a c

b d a w

更新文件应该像:

k l m

x y z ac

bd aw

6 个答案:

答案 0 :(得分:0)

我认为你可以通过以下方式实现:

...
if (s == "a" || s == "b"){
if (s == "a") 
 s = s.Replace("a ", "a");
if (s == "b") 
s = s.Replace("b ", "b");
using (StreamWriter tw = new StreamWriter(@"d:\\My File3.log"))  
 tw.WriteLine(s);
}
...

示例:

 string test="a c";
 test =test.Replace("a ", "a");
 Console.WriteLine(test);

输出: AC

答案 1 :(得分:0)

您在寻找String.Replace吗?

string path = @"d:\My File3.log"; 

var data = File
  .ReadLines(path)
  .Select(line => line
     .Replace("a ", "a")
     .Replace("b ", "b"))
  .ToList(); // Materialization, since we have to write back to the same file

File.WriteAllLines(path, data);

一般情况下,例如

  

如果行包含 Word-to-search

表示ab应为b中的abc不是我们要查找的字词:

"abc a b c a" -> "abc abc a"

尝试使用正则表达式

  string[] words = new string[] { "a", "b" };
  string pattern = 
    @"\b(" + string.Join("|", 
     words.Select(item => Regex.Escape(item))) + 
    @")\s";

  var data = File
   .ReadLines(path)
   .Select(line => Regex.Replace(line, pattern, m => m.Groups[1].Value))
   .ToList();

  File.WriteAllLines(path, data);

答案 2 :(得分:0)

试试这个:

....

while ((line = tr.ReadLine()) != null)
{

    using (StreamWriter tw = new StreamWriter(@"d:\\My File3.log"))
    string st = line.Replace("a ", "a").Replace("b ", "b");//just add additional .Replace() here
    tw.WriteLine(st); 

}

答案 3 :(得分:0)

我认为你的问题在这里:

if (s == "a" || s == "b")
                        s = s.Replace(" ", "");

为了满足您的if条件,string s必须没有任何空格。因此,您的代码什么都不做。

if(s == "a" ||  s == "b")
         foreach(var s2 in items)
         {
            if(items.IndexOf(s2) > items.IndexOf(s) && s2 == " ")
               s2 == string.Empty;
               break;
         }

break的存在是为了确保我们只替换下一个空格,而不是替换该字符后面的所有空格。

答案 4 :(得分:0)

你应该在foreach循环之前考虑一个临时变量

int temp = 0;
foreach(var s in items)
{
    if (temp == 0)
            {
                if (s == "a" || s == "b")
                {
                    temp = 1;        
                }
            }
            else
            {                
                s = s.Replace(" ", "");
                using (StreamWriter tw = new StreamWriter(@"d:\\My File3.log"))  
                tw.WriteLine(s);
                temp = 0;
            }

    }

答案 5 :(得分:0)

您无法在同一次迭代中读取和写入同一文件。 这里有一个使用StringBuilder的解决方案(和他一起操作字符串中的字符):

using (StreamWriter tw = new StreamWriter(@"file1.txt"))
        {


            using (System.IO.TextReader tr = File.OpenText((@"file.txt")))
            {
                string line;
                StringBuilder items = new StringBuilder();
                while ((line = tr.ReadLine()) != null)
                {
                    items.Append(line);
                    items.Replace("a ", "a");
                    items.Replace("b ", "b");
                    tw.WriteLine(items);
                    items.Clear();
                }
            }
        }