c# - .net split - without double quote -
i have csv file below:
id,name,address,phonenumber 101,jack,"no 13, hilltop, london",012346789 102,harry,"no 15, baker street london",012346789 i need read columns (comma separated). when use split function splits address well. want split except address in double quote.
even though using c#, there useful class called textfieldparser in microsoft.visualbasic namespace. need add reference project in addition to using directive:
using microsoft.visualbasic.fileio; then can implement similar follows:
private void parse() { using (textfieldparser parser = new textfieldparser("file.csv") { hasfieldsenclosedinquotes = true, delimiters = new string[] { "," } }) { string[] fields; { fields = parser.readfields(); printresults(fields); } while (fields != null); } } private void printresults(string[] fields) { if (fields != null) { foreach (var field in fields) { console.write(string.concat("[", field, "] ")); } console.writeline(); } } the hasfieldsenclosedinquotes = true property of textfieldparser in case must set achieve desired behavior.
i have placed csv sample data file , run test. data started out (in local file named "file.csv"):
id,name,address,phonenumber 101,jack,"no 13, hilltop, london",012346789 102,harry,"no 15, baker street london",012346789 and resultant output in console calling above parse() method is:
[id] [name] [address] [phonenumber] [101] [jack] [no 13, hilltop, london] [012346789] [102] [harry] [no 15, baker street london] [012346789]
Comments
Post a Comment