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

3
  • 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 How to Ask for good advice on how to present your question in a clear, answerable way. Commented Nov 20, 2015 at 8:44
  • Sorry guys, i've edited my question. I come from a php background and am learning C#. Commented Nov 20, 2015 at 8: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 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. Commented Nov 20, 2015 at 8:48

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

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();

2 Comments

what if there are three items?['Foo', 'bar' , 'baz']
@M.kazemAkhgary: Not sure if that case is realistic. If it is, you would have to change the regular expression.
1
{
    const string oldString = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";
    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<string>>>(oldString);
}

1 Comment

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.
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

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.