-4

I have next string:

'Malaysia Index','Mc\'DONALDS CORPORATION','McDonalds','Me,dia'

And I need array (or list) of string:

Malaysia Index
Mc'DONALDS CORPORATION
McDonalds
Me,dia

EDITED: next code:

var s = "'Malaysia Index','Mc\'DONALDS CORPORATION','McDonalds','Me,dia'";
var ss = s.Split(',');
foreach (var res in ss)
    Console.WriteLine(res);

do result:

'Malaysia Index'
'Mc'DONALDS CORPORATION'
'McDonalds'
'Me                     !!!!!!!!!!!!!!
dia'                    !!!!!!!!!!!!!!
SkyN
  • 1,417
  • 4
  • 17
  • 32
  • 3
    Why Split() is bad solution? – Yuri Dorokhov Nov 19 '15 at 16:49
  • Why not string.split() ? – GregoryHouseMD Nov 19 '15 at 16:49
  • 6
    Looks like a simple CSV type file, and any CSV parser available on NuGet should be able to do what you are asking. – Ron Beyer Nov 19 '15 at 16:49
  • 1
    Possible duplicate of [How to split csv whose columns may contain ,](http://stackoverflow.com/questions/6542996/how-to-split-csv-whose-columns-may-contain) – GregoryHouseMD Nov 19 '15 at 17:03
  • @YuriDorokhov because the delimited data can contain the delimiter. That requires a more complex parsing algorithm than a simple `string.Split`. – juharr Nov 19 '15 at 17:08
  • @juharr, you are right. Ron's answer is correct. – Yuri Dorokhov Nov 19 '15 at 17:11
  • OP can't use Split() method because it produce inappropriate result. – witoong623 Nov 19 '15 at 17:11
  • It's not a CSV file; it's clearly using \' as an escape squence for ' inside of '. CSV has nothing like that. – user5090812 Nov 19 '15 at 17:20
  • @user5090812 there is no standard in csv for a "escape character", using `\'` is perfectly fine, you just need to find a parser that handles it. – Scott Chamberlain Nov 20 '15 at 06:10
  • RFC 4180 specifies the standard for csv, including character escaping considerations, and their behavior does not even remotely resemble "C-style" character escaping. You can write a parser for anything, of course; the difficulty in this case is that we don't have any specification for how it behaves, only guesses. – user5090812 Nov 20 '15 at 16:16

2 Answers2

1

this code produces expected result:

s.Trim('\'')
 .Split(new[]{"','"}, StringSplitOptions.RemoveEmptyEntries)

it removes 1st and last ' symbols and splits by ','

output

Malaysia Index
Mc'DONALDS CORPORATION
McDonalds
Me,dia
ASh
  • 34,632
  • 9
  • 60
  • 82
  • This works as long as the data cannot have spaces between the quotes and delimiting commas and as long as all delimited data elements are enclosed in single quotes. – juharr Nov 19 '15 at 17:14
  • @juharr, i agree. that is likely be true for input strings; a worse thing though is possible `\',\'` inside data fields. they would require a more complex parser – ASh Nov 19 '15 at 17:18
0

Assuming ur text is always in such fomat : 'xxx', 'xxx', ....

do a code to remove the 1st ' and the last '

then replace all ',' to something unique, that would not appear in original text, like 2 pipes ||

now, split the formated text with this new separator ||

and u should get the expected result

Kelmen
  • 1,053
  • 1
  • 11
  • 24