简体   繁体   English

C#-将字符串转换为二维字符串数组

[英]C# - Convert string, to a two dimensional string array

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#? C#中是否没有为此方法内置的方法?

// Thanks // 谢谢

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. 由于您的问题没有给我们提供足够的信息,因此我假设您正在尝试将一些JSON转换为字符串数组。 As far as I know there is no build in method in C# for this. 据我所知,在C#中没有为此内置方法。 You could use an extension for this. 您可以为此使用扩展名。 Newtonsoft JSON 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);

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 ',' array1将包含"'Foo', 'bar'""'Foo', 'bar'""'Foo', 'bar'"然后,您可以将每个元素都用','分隔

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

You could use Regex to get the array elements from your source string and then convert the matches into your arrays. 您可以使用Regex从源字符串中获取数组元素,然后将匹配项转换为数组。 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();

There you can find some examples. 在那里您可以找到一些示例。 The String.Split function returns an array so you can do String.Split函数返回一个数组,因此您可以执行

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 看一下这个线程: multiDimension-array-vs

{
    const string oldString = "[['Foo', 'bar'], ['Foo', 'bar'], ['Foo', 'bar']]";
    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<string>>>(oldString);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM