插入回车每x个字符

时间:2013-11-19 18:27:12

标签: c#

我有一个大文件,没有任何记录参考结束。我需要每X个字符插入一个\ r \ n。当我尝试这个时,长度在第一次插入后被拧紧。

根据记录长度插入\ r \ n的最佳方法是什么?

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以使用Regex.Replace

Regex.Replace(
    longText
,   "(.{5})" // Replace 5 with X
,   "$1\r\n"
)

我们的想法是将固定长度X的子字符串捕获到捕获组编号1中,然后将其替换为该组的内容(表示为$1),后跟{{1} }。

Demo on ideone.

答案 1 :(得分:0)

这样的事应该适合你。它采用由具有给定编码的固定长度记录组成的源文件,并将其转换为“正常”CR + LF分隔记录。当然,隐含的假设是文件包含文本数据。

public static void ConvertFixedLengthRecordsToDelimitedRecords( FileInfo sourceFile , Encoding sourceEncoding , int recordLength )
{
  if ( sourceFile == null ) throw new ArgumentNullException("sourceFile");
  if ( !sourceFile.Exists ) throw new ArgumentException("sourceFile does not exist" , "sourceFile" ) ;
  if ( sourceEncoding == null ) throw new ArgumentNullException("sourceEncoding");
  if ( recordLength < 1 ) throw new ArgumentOutOfRangeException("recordLength");

  // create a temporary file in the temp directory
  string tempFileName = Path.GetTempFileName() ;

  // copy the source file to the temp file 1 record at a time, writing each record as a line.
  //
  // NOTE: If the source data contains '\r', '\n' or '\r\n', you'll get short records in the output
  // You might consider throwing an exception or figuring out how to deal with that.
  using ( FileStream   inStream  = sourceFile.Open( FileMode.Open , FileAccess.Read , FileShare.Read ) ) 
  using ( StreamReader input     = new StreamReader( inStream , sourceEncoding ) )
  using ( Stream       outStream = File.Open( tempFileName , FileMode.Truncate , FileAccess.Write , FileShare.Read ) )
  using ( StreamWriter output    = new StreamWriter( outStream , Encoding.UTF8 ) )
  {
    char[] buf = new char[recordLength];
    int bufl ;

    while ( 0 != (bufl = input.ReadBlock( buf , 0 , buf.Length )) )
    {
      output.WriteLine(buf,0,bufl) ;
    }
  }

  // at this point, the temporary file contains the fixed-length source records at CR+LF terminated lines.
  // We just need to replace the source file with the temp file.
  FileInfo tempFile = new FileInfo( tempFileName ) ;
  string backupFileName = sourceFile.FullName + ".bak" ;
  File.Delete( backupFileName ) ;
  tempFile.Replace( sourceFile.FullName , backupFileName ) ;

  return ;
}