delimited text - Delimit a string by character unless within quotation marks C# -


i need demilitarise text single character, comma. want use comma delimiter if not encapsulated quotation marks.

an example:

method,value1,value2 

would contain 3 values: method, value1 , value2

but:

method,"value1,value2" 

would contain 2 values: method , "value1,value2"

i'm not sure how go when splitting string use:

string.split(','); 

but demilitarise based on commas. possible without getting overly complicated , having manually check every character of string.

thanks in advance

copied comment: use available csv parser visualbasic.fileio.textfieldparser or this or this.

as requested, here example textfieldparser:

var alllinefields = new list<string[]>(); string sampletext = "method,\"value1,value2\""; var reader = new system.io.stringreader(sampletext); using (var parser = new microsoft.visualbasic.fileio.textfieldparser(reader)) {     parser.delimiters = new string[] { "," };     parser.hasfieldsenclosedinquotes = true; // <--- !!!     string[] fields;     while ((fields = parser.readfields()) != null)     {         alllinefields.add(fields);     } } 

this list contains single string[] 2 strings. have used stringreader because sample uses string, if source file use streamreader(f.e. via file.opentext).


Comments

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

android - Linear layout children not scrolling -

javascript - Create websocket without connecting -