<p>I'm looking at parsing a delimited string, something on the order of</p>

<p>a,b,c</p>

<p>But this is a very simple example, and parsing delimited data can get complex; for instance</p>

<p>1,"Your simple algorithm, it fails",True</p>

<p>would blow your naiive string.Split implementation to bits.  Is there anything I can freely use/steal/copy and paste that offers a relatively bulletproof solution to parsing delimited text?  .NET, plox.</p>

<p><em>Update:</em>  I decided to go with the <a href="http://msdn.microsoft.com/en-us/library/f68t4563.aspx" rel="nofollow noreferrer">TextFieldParser</a>, which is part of VB.NET's pile of goodies hidden away in Microsoft.VisualBasic.DLL.</p>

[{'answer_id': 10210, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 0, 'selected': False, 'text': '<p>I am thinking that a generic framework would need to specify between two things:\n1. What are the delimiting characters.\n2. Under what condition do those characters not count (such as when they are between quotes).</p>\n\n<p>I think it may just be better off writing custom logic for every time you need to do something like this.</p>\n'}, {'answer_id': 10217, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 2, 'selected': False, 'text': '<p>I am not aware of any framework, but a simple state machine works:</p>\n\n<ul>\n<li>State 1: Read every char until you hit a " or a ,\n\n<ul>\n<li>In case of a ": Move to State 2</li>\n<li>In case of a ,: Move to State 3</li>\n<li>In case of the end of file: Move to state 4</li>\n</ul></li>\n<li>State 2: Read every char until you hit a "\n\n<ul>\n<li>In case of a ": Move to State 1</li>\n<li>In case of the end of the file: Either Move to State 4 or signal an error because of an unterminated string</li>\n</ul></li>\n<li>State 3: Add the current buffer to the output array, move the cursor forward behind the , and back to State 1.</li>\n<li>State 4: this is the final state, does nothing except returning the output array.</li>\n</ul>\n'}, {'answer_id': 10222, 'author': 'Patrick McElhaney', 'author_id': 437, 'author_profile': 'https://Stackoverflow.com/users/437', 'pm_score': 1, 'selected': False, 'text': '<p>There are some good answers here: <a href="https://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections">Split a string ignoring quoted sections</a></p>\n\n<p>You might want to rephrase your question to something more precise (e.g. <strong>What code snippet or library I can use to parse CSV data in .NET</strong>?). </p>\n'}, {'answer_id': 10223, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 2, 'selected': False, 'text': '<p>Such as</p>\n\n<pre><code>var elements = new List&lt;string&gt;();\nvar current = new StringBuilder();\nvar p = 0;\n\nwhile (p &lt; internalLine.Length) {\n    if (internalLine[p] == \'"\') {\n        p++;\n\n        while (internalLine[p] != \'"\') {\n            current.Append(internalLine[p]);\n            p++;\n        }\n\n        // Skip past last \',\n        p += 2;\n    }\n    else {\n        while ((p &lt; internalLine.Length) &amp;&amp; (internalLine[p] != \',\')) {\n            current.Append(internalLine[p]);\n            p++;\n        }\n\n        // Skip past ,\n        p++;\n    }\n\n    elements.Add(current.ToString());\n    current.Length = 0;\n}\n</code></pre>\n'}, {'answer_id': 10224, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 0, 'selected': False, 'text': '<p>Simplest way is just to split the string into a char array and look for your string determiners and split char. </p>\n\n<p>It should be relatively easy to unit test.</p>\n\n<p>You can wrap it in an extension method similar to the basic .Spilt method.</p>\n'}, {'answer_id': 10262, 'author': 'Jedi Master Spooky', 'author_id': 1154, 'author_profile': 'https://Stackoverflow.com/users/1154', 'pm_score': 3, 'selected': True, 'text': '<p>I use this to read from a file</p>\n\n<pre><code>string filename = @textBox1.Text;\nstring[] fields;\nstring[] delimiter = new string[] {"|"};\nusing (Microsoft.VisualBasic.FileIO.TextFieldParser parser =\n       new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) {\n    parser.Delimiters = delimiter;\n    parser.HasFieldsEnclosedInQuotes = false;\n\n    while (!parser.EndOfData) {\n        fields = parser.ReadFields();\n        //Do what you need\n    }\n}\n</code></pre>\n\n<p>I am sure someone here can transform this to parser a string that is in memory.</p>\n'}, {'answer_id': 10307, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 1, 'selected': False, 'text': '<p>To do a shameless plug, I\'ve been working on a library for a while called <a href="http://www.codeplex.com/fotelo" rel="nofollow noreferrer">fotelo</a>  (Formatted Text Loader) that I use to quickly parse large amounts of text based off of delimiter, position, or regex. For a quick string it is overkill, but if you\'re working with logs or large amounts, it may be just what you need. It works off a control file model similar to SQL*Loader (kind of the inspiration behind it).</p>\n'}, {'answer_id': 265861, 'author': 'rohancragg', 'author_id': 5351, 'author_profile': 'https://Stackoverflow.com/users/5351', 'pm_score': 2, 'selected': False, 'text': '<p>A very complrehesive library can be found here: <a href="http://www.filehelpers.com/" rel="nofollow noreferrer">FileHelpers</a></p>\n'}, {'answer_id': 6533735, 'author': 'gjvdkamp', 'author_id': 65747, 'author_profile': 'https://Stackoverflow.com/users/65747', 'pm_score': 1, 'selected': False, 'text': '<p>Better late than never (add to the completeness of SO):</p>\n\n<p><a href="http://www.codeproject.com/KB/database/CsvReader.aspx" rel="nofollow">http://www.codeproject.com/KB/database/CsvReader.aspx</a></p>\n\n<p>This one ff-ing rules. </p>\n\n<p>GJ</p>\n'}]
['https://Stackoverflow.com/questions/10205', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']