1

I can't seem to figure this out.

I want to convert this:

string foobarString = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";

To a two dimensional array (or list) so it looks like this:

fooBarArray[0] = Array['Foo', "bar"];
fooBarArray[1] = Array['Foo', "bar"];
fooBarArray[2] = Array['Foo', "bar"];
... etc.

I have tried spliting by ("],") and then cleaning the string and creating an array afterwards. But it's just to damn UGLY!

I want a cleaner version. Is there no method built in for such a method in C#?

// Thanks

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Hans Preutz
  • 679
  • 2
  • 10
  • 28
  • It is not clear what you are asking, because you're not starting from correct C# code. Please edit your post so that your question can be understood. See also [ask] for good advice on how to present your question in a clear, answerable way. – Peter Duniho Nov 20 '15 at 08:44
  • Sorry guys, i've edited my question. I come from a php background and am learning C#. – Hans Preutz Nov 20 '15 at 08:45
  • 2
    _" Is there no method built in for such a method in C#?"_ -- why would there be? Please improve your question by including [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that shows clearly what you've tried, with a precise explanation of what the code does and how that's different from what you want. – Peter Duniho Nov 20 '15 at 08:48

5 Answers5

4

Since your question is not giving us enough information I will assume that you are trying to convert some JSON into an array of strings. As far as I know there is no build in method in C# for this. You could use an extension for this. Newtonsoft JSON

After installing this package you will be able to use the following code:

string foobarString = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<string[][]>(foobarString);
Beniamin E.
  • 156
  • 7
3

First, split it by "[[", "], [", "]]"

var array1 = foobarString.Split(new string[] {"[[", "], [", "]]"}, StringSplitOptions.RemoveEmptyEntries);

array1 will contain "'Foo', 'bar'", "'Foo', 'bar'", "'Foo', 'bar'" Then you can split every element by ','

var fooBarArray = array1.Select(x => x.Split(',').ToArray()).ToArray()

You can do it in one line

var fooBarArray = foobarString.Split(new string[] { "[[", "], [", "]]" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Split(',').ToArray()).ToArray()
Valentin
  • 5,380
  • 2
  • 24
  • 38
1

You could use Regex to get the array elements from your source string and then convert the matches into your arrays. Something like this should do the trick:

  var input = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";

  // search for [' more than one word character, put them into group a ', 
  //   more than one whitespace ' more than one word character, put them into group b ']
  var arrayMatches = Regex.Matches(input, @"\['(?<a>[\w]+)',\s+'(?<b>[\w]+)'\]");

  var arrays = new List<string[]>();
  foreach (Match arrayMatch in arrayMatches)
  {
    // if the match was unsuccessful, take the next match
    if(!arrayMatch.Success)
      continue;

    // create a new string array with element in group a as first element and the element in groub b as the second one
    var array = new [] {arrayMatch.Groups["a"].Value, arrayMatch.Groups["b"].Value};
    arrays.Add(array);
  }

  // convert list to array
  return arrays.ToArray();
Chrisi
  • 371
  • 3
  • 15
1
{
    const string oldString = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";
    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<string>>>(oldString);
}
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Bono Nov 20 '15 at 21:49
0

There you can find some examples. The String.Split function returns an array so you can do

string[] lines = foobarString.Split(new char[]{']'}, StringSplitOptions.RemoveEmtpyEntries);
fooBarArray[0] = lines[i].Split(new char[]{'[',']', ','}, StringSplitOptions.RemoveEmtpyEntries);

Take a look at this thread: multidimensional-array-vs

Community
  • 1
  • 1