vb.net .NET 中的 TextFieldParser 等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20456055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextFieldParser equivalent in .NET?
提问by ElektroStudios
Is there a modern .NET equivalent to the TextFieldParserclass in VB6? Performance is a lot lower than a simple String.Split()
是否有TextFieldParser与 VB6 中的类等效的现代 .NET ?性能比简单的低很多String.Split()
回答by Vyacheslav Yudanov
I have compared performance with that code: https://gist.github.com/Ruszrok/7861319
我将性能与该代码进行了比较:https: //gist.github.com/Ruszrok/7861319
I used an input file with about 1 000 000 records separated with spaces. I tried five experiments.
我使用了一个输入文件,其中包含大约 1 000 000 条记录,并用空格分隔。我尝试了五个实验。
String.Splitavg time: 291 msMicrosoft.VisualBasic.FileIO.TextFieldParseravg time: 15843 ms
String.Split平均时间:291 毫秒Microsoft.VisualBasic.FileIO.TextFieldParser平均时间:15843 毫秒
You can use the Microsoft.VisualBasic.FileIO.TextFieldParserclass. Reference Microsoft.VisualBasic. Sample in gist.
您可以使用Microsoft.VisualBasic.FileIO.TextFieldParser该类。参考Microsoft.VisualBasic。要点示例。
回答by Kevin Diaz
This is my solution:
这是我的解决方案:
public class TextFieldParser
{
enum FieldType { FixedWidth, Delimited };
public enum CompleteElements
{
/// <summary>
/// Returns as many elements as fileWidths input be
/// </summary>
AllElements,
/// <summary>
/// Only returns elements who have not null values
/// </summary>
OnlyValues
};
int[] m_fieldWidths;
string m_line;
List<string> m_results;
int m_lineWidth;
public CompleteElements m_CompleteElements;
public TextFieldParser(string line)
{
m_line = line;
m_lineWidth = m_line.Length;
m_results = new List<string>();
m_CompleteElements = CompleteElements.OnlyValues;
}
public void SetCompleteElements(CompleteElements value)
{
m_CompleteElements = value;
}
public void SetFieldWidths(params int[] fileWidths)
{
m_fieldWidths = fileWidths;
}
public string[] ReadFields()
{
int pivot = 0;
m_results = new List<string>();
for (int x = 0; x < m_fieldWidths.Length; x++)
{
if(pivot + m_fieldWidths[x] <= m_lineWidth)
{
m_results.Add(m_line.Substring(pivot, m_fieldWidths[x]));
}
else
{
if (m_CompleteElements == CompleteElements.AllElements)
{
m_results.Add(null);
}
break;
}
pivot += m_fieldWidths[x];
}
return m_results.ToArray();
}
}
A simple session:
一个简单的会话:
string line = "1234567890123456789012345678890";
TextFieldParser parser = new TextFieldParser(line);
parser.SetFieldWidths(1, 2, 3, 4, 5, 6, 7, 8);
string[] resultOnlyValues = parser.ReadFields();
/*
results:
resultOnlyValues[0] : "1"
resultOnlyValues[1] : "23"
resultOnlyValues[2] : "456"
resultOnlyValues[3] : "7890"
resultOnlyValues[4] : "12345"
resultOnlyValues[5] : "678901"
resultOnlyValues[6] : "2345678"
*/
parser.SetCompleteElements(TextFieldParser.CompleteElements.AllElements);
string[] resultAllElement = parser.ReadFields();
/*
results:
resultAllElement[0] : "1"
resultAllElement[1] : "23"
resultAllElement[2] : "456"
resultAllElement[3] : "7890"
resultAllElement[4] : "12345"
resultAllElement[5] : "678901"
resultAllElement[6] : "2345678"
resultAllElement[7] : null
*/

