[英]C# so I need to split out a string, I think
so I have this application that I have inherited from someone that is long gone. 所以我有一个已经从一个久违的人那里继承下来的应用程序。 The gist of the application is that it reads in a .cvs file that has about 5800 lines in it, copies it over to another .cvs, which it creates new each time, after striping out a few things , #, ', &. 该应用程序的要旨是,它读取其中包含约5800行的.cvs文件,然后将其复制到另一个.cvs中,在剔除一些#,'和&之后,它每次都会创建一个新的.cvs文件。 Well everything works great, or it has until about a month ago. 好吧,一切都很好,或者直到一个月前才奏效。 so I started checking into it, and what I have found so far is that there are about 131 items missing from the spreadsheet. 所以我开始检查它,到目前为止,我发现电子表格中大约缺少131个项目。 Now I read someplace that the maximun amount of data a string can hold is over 1,000,000,000 chars, and my spreadsheet is way under that, around 800,000 chars, but the only thing I can think is doing it is the string object. 现在,我在某个地方读到一个字符串可以容纳的最大数据量超过1,000,000,000个字符,而我的电子表格正处于该状态下,大约为80万个字符,但是我唯一能想到的就是字符串对象。
So anyway, here is the code in question, this piece appears 所以无论如何,这是有问题的代码,这部分出现了
to both read in from the existing field, and output to the new file: 从现有字段读入,并输出到新文件:
StreamReader s = new StreamReader(File);
//Read the rest of the data in the file.
string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.
string[] rows = AllData.Split("\r\n".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
//Now add each row to the DataSet
foreach (string r in rows)
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());
//Add the item
result.Rows.Add(items);
}
If anyone can help me I would really appreciate it. 如果有人可以帮助我,我将非常感激。 I either need to figure out how to split the data better, or I need to figure out why it is cutting out the last 131 lines from the existing excel file to the new excel file. 我或者需要弄清楚如何更好地拆分数据,或者我需要弄清楚为什么要从现有的excel文件中删除最后131行到新的excel文件。
One easier way to do this, since you're using "\\r\\n"
for lines, would be to just use the built-in line reading method: File.ReadLines(path)
一种简单的方法是使用"\\r\\n"
作为行,因此只需使用内置的行读取方法: File.ReadLines(path)
foreach(var line in File.ReadLines(path))
{
var items = line.Split(',');
result.Rows.Add(items);
}
You may want to check out the TextFieldParser
class, which is part of the Microsoft.VisualBasic.FileIO
namespace (yes, you can use this with C# code) 您可能想签出TextFieldParser
类,该类是Microsoft.VisualBasic.FileIO
命名空间的一部分(是的,您可以将其与C#代码一起使用)
Something along the lines of: 类似于以下内容:
using(var reader = new TextFieldParser("c:\\path\\to\\file"))
{
//configure for a delimited file
reader.TextFieldType = FieldType.Delimited;
//configure the delimiter character (comma)
reader.Delimiters = new[] { "," };
while(!reader.EndOfData)
{
string[] row = reader.ReadFields();
//do stuff
}
}
This class can help with some of the issues of splitting a line into its fields, when the field may contain the delimiter. 当字段可能包含定界符时,此类可以帮助解决将行拆分为字段的某些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.