I am parsing a text file from some old hardware and I need to extract an array of strings from one long string.
- Elements are comma-separated
- Each individual string is enclosed in single quotes
'Hello', 'World'
{"'Hello', "'World'"}
Single quotes for any other reason are illegal and there is no escaping character equivalent; you cannot write the word "Bob's" nor can do something like "Bob\'s".
Ordinarily it would be easy to get the individual elements by splitting on the comma:
string testString = "'Hello', 'World'";
ArrayList result = new ArrayList(testString.Split(',');
But I can't do this; if a comma is in-between single quotes it is text, not a separator.
This is one element:
'Hello, World'
{"'Hello, World'"}
How can I extract the elements checking to see if the comma is in-between single quotes?
'Hello', 'World', 'Hello, World'
{"'Hello'", "'World'", "'Hello, World'"}
One more detail: I cannot guarantee the amount of whitespace between elements:
'Hello', 'World', 'Hello, World'
P.S. Here is the same question I asked for Swift: Swift: Split comma-separated strings, but not if comma enclosed in single quotes