0

Given this string:

"['some value','another value','value three']"

What is the most efficient way of converting it to an array? Or any enumerable-type, for that matter?

EDIT: The array would contain the inner string values:

arr[0] = some value
arr[1] = another value
arr[2] = value three
2
  • and what will be the values of array? Commented Jul 30, 2014 at 5:23
  • 1
    string [] a = s.Split(new string[]{"['", "','", "']"}, StringSplitOptions.RemoveEmptyEntries); Commented Jul 30, 2014 at 5:24

4 Answers 4

4

It's json-array, use http://james.newtonking.com/json for this

 public static T JsonDeserialize<T> (string jsonString)
 {
     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
     MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
     T obj = (T)ser.ReadObject(ms);
     return obj;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

That is the most elegant and comfortable way, but it requires installing additional libraries - Newtonsoft.
yup, but it's library is most popular and solves many(i think all :)) problems with processing js data in .NET
0

This is just a possible way:

        var str = "['some value','another value','value three']";

        // eliminate '[' and ']'
        str = str.Substring(1, str.Length - 2);

        // remove '
        str = str.Replace("'", string.Empty);

        // split it in a array
        var items = str.Split(',');

For further information about splitting strings, you can use die MSDN

Hope it helps.

2 Comments

This assumes the data will not contain any apostrophes or commas, which may or may not be an acceptable assumption.
@Sahuagin Yes, you are right, this is the example's behavior. This is what Nathan asked for. Anyway a general solution could be preferably if it is really needed.
0

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

Comments

0
string s = "['some value','another value','value three']";
s=s.Replace("[", "").Replace("'","").Replace ("]","") ; 
List<string> names = s.Split(',').ToList<string>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.