加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 替换第n个位置的字符

发布时间:2020-12-15 04:24:02 所属栏目:百科 来源:网络整理
导读:我正在编写这个程序来替换文本文件中字符串第n个位置的字符.我的文本文件包含以下内容 – the quick brown fox jumped over the lazy dogthe quick brown fox jumped over the lazy dogthe quick brown fox jumped over the lazy dogthe quick brown fox jum
我正在编写这个程序来替换文本文件中字符串第n个位置的字符.我的文本文件包含以下内容 –
the quick brown fox jumped over the lazy dog
the quick brown fox jumped over the lazy dog
the quick brown fox jumped over the lazy dog
the quick brown fox jumped over the lazy dog

这是代码的输出 –

thehuick brown fox jumped over the lazy dog

以上结果不是我想要的.只更新了一行,其余的行不再在文件中找到.

这是我在C#中的完整代码

var txtFiles = Directory.GetFiles(@"E:PROJreplaceYreplaceY","*.txt");
        foreach (string currentFile in txtFiles)
        {
            string[] lines = File.ReadAllLines(currentFile);
            foreach (string line in lines)
            {

                var theString = line;
                var aStringBuilder = new StringBuilder(theString);
                aStringBuilder.Remove(3,2);
                aStringBuilder.Insert(3,"h");
                theString = aStringBuilder.ToString();
                using (StreamWriter outfile = new StreamWriter(currentFile))
                {
                    outfile.Write(theString.ToString());
                }

                Console.WriteLine(theString);
                Console.ReadKey();

            }
        }

我哪里出错了?请帮忙!

解决方法

因为你的逻辑说:

>为每个文件

>打开文件
>对于文件中的每一行

>处理它
>重新打开文件进行写入
>写下这一行

这意味着您反复使用最新行覆盖该文件.最后,每个文件只包含一行:最后一行.

你会想做这样的事情:

static void Main(string[] args)
{
  foreach ( string fileName in args )
  {
    string tempFileName = Path.GetTempFileName() ;

    using ( Stream       tmpFile = File.Open( tempFileName,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None ) )
    using ( StreamWriter output = new StreamWriter( tgtFile,Encoding.Default ) )
    using ( Stream       srcFile = File.Open( fileName,FileMode.Open,FileAccess.ReadWrite,FileShare.None ) )
    using ( StreamReader input = new StreamReader( srcFile,Encoding.Default,true ) )
    {
      string line ;
      while ( null != (line=input.ReadLine()) )
      {
        output.Write( line.Substring(0,3) ) ;
        output.Write( 'h' ) ;
        output.WriteLine( line.Substring(5) ) ;
      }
    }

    string backupFileName = string.Format( "{0}.{1:yyyy-MM-dd.HHmmss}.bak",fileName,DateTime.Now ) ;

    File.Move( fileName,backupFileName ) ;
    File.Move( tempFileName,fileName ) ;
    File.Delete( backupFileName ) ;

  }
  return;
}

您需要添加一些try / catch / finally异常处理来处理文件向南的情况,这样您就可以将该文件回滚到其原始状态.

其他选择是

>打开文件进行阅读.
>使用File.ReadAllLines()或等效文件覆盖整个内容
>根据需要变换线条
>重新打开原始文件进行写入
>将转换后的内容吐出到原始文件中.

这会带给你这样的东西:

static void Main(string[] args)
{
  foreach ( string fileName in args )
  {
    string[] lines = File.ReadAllLines( fileName ) ;

    for ( int i = 0 ; i < lines.Length ; ++i )
    {
      lines[i] = ApplyTransformHere( lines[i] ) ;
    }

    File.WriteAllLines( fileName,lines ) ;

  }

  return;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读