本文介绍了有效地更新sting fast anf中的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨我必须按照一定的标准更新文件的行。 前2行,后2行与所有内容不同介于两者之间。 所以我有这段代码可以正确更新行,但是当用户插入一个巨大的文件(不超过约5MB) 所以我需要一种更快的方式来更新前两行和最后两行然后介于两者之间的所有内容然后再修改文件内容发生了变化。 删除和插入是精确和准确的我只需要更快地运行数组并更快地更新。 public string ChangeFileDates( string filetoChange,DateTime changeDate) { try { string str = changeDate.ToString( YYMMDD); string revisedFile = null ; string [] lines = filetoChange.Split( new string [] {Environment.NewLine},StringSplitOptions.None); // 所以问题来自这里 for ( int i = 0 ; i < lines.Length; i ++) { string myline = lines [i]。的ToString(); if (i == 0 ) { myline = myline.Remove( 28 , 6 ); myline = myline.Insert( 28 ,str); revisedFile = revisedFile + myline; } else if (i == 1 ) { myline = myline.Remove( 12 , 18 ); myline = myline.Insert( 12 ,str); myline = myline.Insert( 18 ,str); myline = myline.Insert( 24 ,str); modifiedFile = revisedFile + Environment.NewLine + myline; } else if (i ==(lines.Length - 1 )) { myline = myline.Remove( 28 , 6 ); myline = myline.Insert( 28 ,str); modifiedFile = revisedFile + Environment.NewLine + myline; } else if (i ==(lines.Length - 2 )) { myline = myline.Remove( 18 , 12 ); myline = myline.Insert( 18 ,str); myline = myline.Insert( 22 ,str); modifiedFile = revisedFile + Environment.NewLine + myline; } else { myline = myline.Remove( 58 , 6 ); myline = myline.Insert( 58 ,str); modifiedFile = revisedFile + Environment.NewLine + myline; } } return revisedFile; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null ; } } 解决方案 HiI have to update the lines of a file according to certain standards.the first 2 lines and the last 2 lines differ from everything in between.so i have this piece of code that will update the lines correctly, but the problem comes in when a user inserts a huge file (no more than about 5MB)so i need a faster way of updating the first 2 rows and the last 2 rows and then everything in between and then amend the file back as it was with the content changed.the remove and inserts are precise and accurate I just need to run through the array faster and update faster. public string ChangeFileDates(string filetoChange, DateTime changeDate) { try { string str = changeDate.ToString("yyMMdd"); string amendedFile = null; string[] lines = filetoChange.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); //so the problem comes here for (int i = 0; i < lines.Length; i++) { string myline = lines[i].ToString(); if (i == 0) { myline = myline.Remove(28, 6); myline = myline.Insert(28, str); amendedFile = amendedFile + myline; } else if (i == 1) { myline = myline.Remove(12, 18); myline = myline.Insert(12, str); myline = myline.Insert(18, str); myline = myline.Insert(24, str); amendedFile = amendedFile + Environment.NewLine + myline; } else if (i == (lines.Length - 1)) { myline = myline.Remove(28, 6); myline = myline.Insert(28, str); amendedFile = amendedFile + Environment.NewLine + myline; } else if (i == (lines.Length - 2)) { myline = myline.Remove(18, 12); myline = myline.Insert(18, str); myline = myline.Insert(22, str); amendedFile = amendedFile + Environment.NewLine + myline; } else { myline = myline.Remove(58, 6); myline = myline.Insert(58, str); amendedFile = amendedFile + Environment.NewLine + myline; } } return amendedFile; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } } 解决方案 这篇关于有效地更新sting fast anf中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 15:31