2016-09-05 76 views
0

有谁知道一个C#中的等价偶尔PHP任务的:转换点符号串多维数组

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

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

我假设字典需要保存object类型的项目,如果它是最后一个项目,我以后会将它们投射到Dictionary<string, string>string

+0

我认为这将需要一定程度的反思来实现这一点。可能有助于说明这个用例,因为可能有其他解决方案来解决您的问题 –

+0

您是否需要将1个字符串解析为字典,还是存在更复杂的用例?只需拆分文本可以很简单,就像这样https://dotnetfiddle.net/NEbUK7 – Icepickle

+0

@Ippickle否,列表中的所有字符串。如果循环遇到现有的键,那么它应该将该字符串附加到现有的子词典(希望这有意义?)。 – silkfire

回答

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

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