Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How divide a string into array

Tags:

json

c#

If I have the following plain string, how do I divide it into an array of three elements?

{["a","English"],["b","US"],["c","Chinese"]}

["a","English"],["b","US"],["c","Chinese"]

This problem is related to JSON string parsing, so I wonder if there is any API to facilitate the conversion.

like image 567
Ricky Avatar asked Mar 14 '26 01:03

Ricky


2 Answers

use DataContract serialization http://msdn.microsoft.com/en-us/library/bb412179.aspx

like image 120
Ian Mercer Avatar answered Mar 15 '26 15:03

Ian Mercer


I wrote a little console example using regex there is most likely a better way to do it.

static void Main(string[] args)
    {
        string str = "{[\"a\",\"English\"],[\"b\",\"US\"],[\"c\",\"Chinese\"]}";
        foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(str, @"((\[.*?\]))"))
        {
            Console.WriteLine(m.Captures[0]);
        }
    }
like image 42
rerun Avatar answered Mar 15 '26 15:03

rerun