0

Possible Duplicate:
C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas

I am actually processing a text file that the data is like that

    //Sample Structure = 'Name','Address','TelNumber'
    Sting OriginalData= "'Mr Worgon','27,4 Streeat A,US', '60231212'";

I am try with

    String[] SampleData;
    String ABC1,ABC2,ABC3;

    SampleData = OriginalData.Split(',');
    ABC1 = SampleData[0];
    ABC2 = SampleData[1];
    ABC3 = SampleData[2];

but its seem it not suitable because of the Address Data that mostly will have a ','.

Any suggestion for this kind of String controlling?

Community
  • 1
  • 1
Worgon
  • 1,557
  • 5
  • 22
  • 27
  • 4
    Don't use `String.Split` to parse a csv file but use one of the available csv-parsers. The [`TextFieldParser`](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx) is already available in the framework(VisualBasic.dll but can be used also with C#). – Tim Schmelter Nov 28 '12 at 13:40
  • Try @"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*". It will provide the output you are expecting. It is from the link, David specified. – Tilak Nov 28 '12 at 13:53
  • DavidBrunow link not useful? – MMK Nov 28 '12 at 14:10

1 Answers1

-1

Can do like:

string OriginalData= "'Mr Worgon','27,4 Streeat A,US', '60231212'";
var split = OriginalData.Split('\'');

and after

ABC1 = SampleData[1];
ABC2 = SampleData[3];
ABC3 = SampleData[5];

in other words, every second splitted array element, in this concrete case.

Tigran
  • 61,654
  • 8
  • 86
  • 123