有效地更新sting fast anf中的每一行 [英] updateing every line in a sting fast anf effectively

查看:52
本文介绍了有效地更新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 ;
}



}

解决方案

有这样的这里有很多你不应该做的事情,最好重新开始吧!

当你开始使用C#时你几乎肯定被告知字符串是不可变的 - 一旦创建它们就无法更改。因此,每次将两个字符串一起添加时,通过分配空间并将字符串信息复制到其中来创建新的较长字符串。这不是一个快速的过程,特别是当你开始查看非常长的字符串时:你正在考虑复制兆字节的数据以将每一行附加到输出。因此,第一个改进是使用StringBuilder。这应该可以立即带来性能提升。



但坦率地说,无论如何我都不会这样做。相反,我会使用正则表达式 - 它们的目的是应对文本替换,并且可以更快,更快地完成这个过程。

所以我要做的就是使用 string.IndexOf [ ^ ]查找换行符,并对最后2行执行相同操作(使用< a href =http://msdn.microsoft.com/en-us/library/1tw91fa3.aspx> string.LastIndexOf [ ^ ])然后通过正则表达式运行整个中间位置来进行替换。





只是一个正则表达式的例子,你还会生成一个字符串列表吗?如果是这样的话,请给我一个索引的例子。





如果您使用C#进行编码一年,你应该知道这些东西 - 这是非常基本的。



正则表达式是一个相当简单的:

正则表达式正则表达式= 正则表达式(  ^(。{28})(\\\\ {6})(。*)


,RegexOptions.Multiline | RegexOptions.Compiled);
string inputText = abcdefghijklmnopqrstuvwxyzab445566ghjk\\\
abcdefghijklmnopqrstuvwxyzab445566ghjk ;
string result = regex.Replace(inputText,

1xxxxxx

Hi I 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;
           }



           }

解决方案

There are just so many things here that you shouldn''t do, that it is probably best to start again!
You were almost certainly told when you started with C# that strings are immutable - they cannot be changed once they are created. So every time you add two strings together, a new longer string is created by allocating the space and copying the string info into it. This is not a quick process, particularly when you start to look at very long strings: and you are looking at copying megabytes of data to append each line to your output. So the first improvement would be to use a StringBuilder instead. This should give you an immediate performance gain.

But frankly, I wouldn''t do it that way anyway. Instead, I would use a Regex - they are designed to cope with text replacement and can do the process much, much quicker.
So what I would do is extract the first too lines manualy, using string.IndexOf[^] to find the newlines, and do the same with the final 2 rows (using string.LastIndexOf[^]) and then run whole of the middle through a Regex to do the replacement.


"well just an example of a regex and would you still generate a string list or not? and if so just an example of the indexing thing please."


If you have been coding in C# for a year, you should know this stuff - it''s pretty basic.

The regex is a fairly simple one:

Regex regex = new Regex("^(.{28})(\\d{6})(.*)


", RegexOptions.Multiline | RegexOptions.Compiled); string inputText = "abcdefghijklmnopqrstuvwxyzab445566ghjk\nabcdefghijklmnopqrstuvwxyzab445566ghjk"; string result = regex.Replace(inputText, "


1xxxxxx


这篇关于有效地更新sting fast anf中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆