将点表示法字符串转换为多维数组

时间:2016-09-05 12:05:21

标签: c# multidimensional-array

有没有人知道这个偶然的PHP任务的C#等价物:

Convert dot syntax like "this.that.other" to multi-dimensional array in PHP

也就是说,将像level1.level2.level3 = item这样的字符串列表转换为字典或多维数组?

我假设字典需要保存object类型的项目,之后我会将它们转换为Dictionary<string, string>string,如果它是&#39;最后一项。

2 个答案:

答案 0 :(得分:0)

这样的代码有用吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "level1.level2.level3 = item";

            string pattern = "^(?'keys'[^=]+)=(?'value'.*)";

            Match match = Regex.Match(input, pattern);

            string value = match.Groups["value"].Value.Trim();
            string[] keys = match.Groups["keys"].Value.Trim().Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries);

            Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

            foreach (string key in keys)
            {
                if (dict.ContainsKey("key"))
                {
                    dict[key].Add(value);
                }
                else
                {
                    dict.Add(key, new List<string>() { value });
                }
            }

        }
    }
}

答案 1 :(得分:0)

我想你可以这样做,但是(我确定可以做更多的优化)

using System;
using System.Collections.Generic;

public class Program
{
    public class IndexedTree {
        private readonly IDictionary<string, IndexedTree> _me;
        private object _value;
        private readonly string _splitKey = ".";

        public IndexedTree this[string key] {
            get {
                return _me[key];
            }
        }

        public object Value { get; set; }

        public void Add(string dottedItem) {
            if ( string.IsNullOrWhiteSpace( dottedItem ) ) {
                throw new ArgumentException("dottedItem cannot be empty");
            }
            int index;
            if ( (index = dottedItem.IndexOf( _splitKey ) ) < 0 ) {
                throw new ArgumentException("dottedItem didn't contain " + _splitKey);
            }
            string key = dottedItem.Substring(0, index), rest = dottedItem.Substring(index + 1);
            IndexedTree child;
            if (_me.ContainsKey(key)) {
                child = _me[key];
            } else {
                child = new IndexedTree( _splitKey );
                _me.Add(key, child);
            }
            if (rest.IndexOf(_splitKey) >= 0) {
                child.Add(rest);
            } else {
                // maybe it can be checked if there is already a value set here or not
                // in case there is a warning or error might be more appropriate
                child.Value = rest;
            }
        }

        public IndexedTree(string splitKey) {
            _splitKey = splitKey;
            _me = new Dictionary<string, IndexedTree>();
        }
    }

    public static void Main()
    {
        IndexedTree tree = new IndexedTree(".");
        tree.Add("Level1.Level2.Level3.Item");
        tree.Add("Level1.Level2.Value");
        Console.WriteLine(tree["Level1"]["Level2"].Value);
        Console.WriteLine(tree["Level1"]["Level2"]["Level3"].Value);
    }
}

你可以在这里看到结果: https://dotnetfiddle.net/EGagoz