Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

c# - TextFieldParser equivalent in .NET?

Is there a modern .NET equivalent to the TextFieldParser class in VB6? Performance is a lot lower than a simple String.Split()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...