Well, if you can assume that each item will not have a comma, then it could be as straight forward as:
const string value = "['some value','another value','value three']";
var array =
value
.Substring(1, value.Length - 2)
.Split(',')
.Select(item => item.Substring(1, item.Length - 2))
.ToArray();
If you can't assume there are no commas, but can assume there are no sequences of apostrophe-comma-apostrophe, then you could do this:
const string value = "['some value','another value','value three']";
var array =
value
.Substring(1, value.Length - 2)
.Split(new[] { "','" }, StringSplitOptions.RemoveEmptyEntries)
.ToArray();
array[0] = array[0].Substring(1, array[0].Length - 1);
int x = array.Length - 1;
array[x] = array[x].Substring(0, array[x].Length - 1);
Otherwise your options would be:
- 'manually' loop through the data, keeping track of what's inside of each pair of apostrophes
- using a regex, though I'm not sure how well that would work
- as Igor has said, use a JSON deserializer, if this is indeed something that could be deserialized that way
string [] a = s.Split(new string[]{"['", "','", "']"}, StringSplitOptions.RemoveEmptyEntries);