Differentiated line parsing?

I am considering parsing a delimited string, something of the order

a B C

But this is a very simple example, and delimiting delimited data can be difficult; eg

1, "Your simple algorithm, it does not work," True

will hit your naiive string.Split implementation in bits. Is there anything that I can freely use / steal / copy and paste that offers a relatively bulletproof solution for delimited text parsing? .NET, plox.

Update: I decided to go with TextFieldParser , which is part of a bunch of VB.NET pros hidden in Microsoft.VisualBasic. Dll.

+3
source share
9 answers

I use this to read from a file

string filename = @textBox1.Text;
string[] fields;
string[] delimiter = new string[] {"|"};
using (Microsoft.VisualBasic.FileIO.TextFieldParser parser =
       new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) {
    parser.Delimiters = delimiter;
    parser.HasFieldsEnclosedInQuotes = false;

    while (!parser.EndOfData) {
        fields = parser.ReadFields();
        //Do what you need
    }
}

, - , , .

+4

- , :

  • 1: char, "",
    • ": 2
    • a: 3
    • : 4
  • 2: char, "
    • ": 1
    • : 4, -
  • 3: , 1.
  • 4: , , .
+2

,

var elements = new List<string>();
var current = new StringBuilder();
var p = 0;

while (p < internalLine.Length) {
    if (internalLine[p] == '"') {
        p++;

        while (internalLine[p] != '"') {
            current.Append(internalLine[p]);
            p++;
        }

        // Skip past last ',
        p += 2;
    }
    else {
        while ((p < internalLine.Length) && (internalLine[p] != ',')) {
            current.Append(internalLine[p]);
            p++;
        }

        // Skip past ,
        p++;
    }

    elements.Add(current.ToString());
    current.Length = 0;
}
+2
+2

: ,

, - (, , CSV- .NET?).

+1

, fotelo ( ), , , . , , , . , SQL * Loader ( ).

+1

I think that the general structure should indicate between two things: 1. What are the separation characters. 2. Under what conditions these characters are not taken into account (for example, when they are between quotation marks).

I think it is best to write custom logic for every time you need to do something like this.

0
source

The simplest way is to simply split the string into a char array and find the string parameters and split the char.

This should be relatively easy unit test.

You can wrap it in an extension method similar to the basic .Spilt method.

0
source

Source: https://habr.com/ru/post/1696411/


All Articles