简体   繁体   English

C#,所以我认为我需要拆分一个字符串

[英]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.

相关问题 我如何分割字符串C# - how can i split a string C# C#为什么我不能拆分字符串? - C# Why i can not split the string? 将网址视为C#对象,以便我可以拆分参数 - Treat url as c# object so I can split params 我需要在拆分方法 c# 后将字符串分配给数组列 - I need to assign string to array columns after split method c# 如何从字符串中拉出变量我需要与另一个预先存在的字符串C#进行比较 - How to pull a variable out of a string I need to compare to another, pre-existing string c# 为什么这个 C# 代码乱序执行? 不是 ASYNC(至少我认为不是) - Why is this C# code executing out of sequence? Not ASYNC (at least I don't think it is) 我有一个C ++代码,需要转换为C#,差不多完成了(我认为),但是需要P / Invoke的帮助才能使用非托管dll。 - I have a c++ code that needs to be translated to c#, almost done (I think) but need help with P/Invoke to use an unmanaged dll C#字符串拆分-索引超出范围 - C# string split - index out of bounds C# “这里的字符串可能是 null” 但我不认为它可能是 null - C# “String may be null here” but I don't think it can be null C# - 为什么我需要初始化[Out]参数 - C# - Why do I need to initialize an [Out] parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM