拆分和分组字符串

时间:2015-05-09 14:02:44

标签: c# html asp.net

我正在使用ASP.NET Webforms,并且有几个C#字符串代表父类和子类。字符串在这个表格上:

parent>child

我想分割字符串并将每个孩子列在正确的父母下面。我的最终目标是能够在我的网页上输出这些父类别和子类别,如下所示:

<li>Parent
  <ul>Child 1</ul>
  <ul>Child 2</ul>
</li>

例如,如果我有以下字符串:

string cat1 = "fish>shark";
string cat2 = "fish>nemo";
string cat3 = "birds>eagle";
string cat4 = "birds>hawk";

我想在我的页面上显示它们:

  • 鱼     
        
      尼莫
        
  • 鸟     
        
      霍克
        
  • 实现这一目标的最佳方法是什么?

    2 个答案:

    答案 0 :(得分:3)

    我会创建一个列表哈希表:

     Dictionary<string, List<string>> data;
    

    然后我会循环遍历所有字符串并将它们放在结构中:

     foreach (var pair in stringList)
     {
          string[] split = pair.split('>');
          if (data.ContainsKey(split[0]) == false)
              data.Add(split[0], new List<string>());
    
          data[split[0]].Add(split[1]);
     }
    

    这样的事情应该有用。我没有编译它所以你可能需要做一些调整,但它应该给你基本的想法。

    输出数据:

     foreach (string parent in data.Keys)
     {
          // Output parent data
    
          foreach (string child in data[parent])
          {
               // Output each child.
          }
     }
    

    答案 1 :(得分:2)

    我不确定您是否只想将标记创建为字符串,或者您是否希望将列表及其子项作为ASP.NET控件执行。无论如何,这是一个选项:

    HtmlGenericControl rootUl, parentLi, childLi;
    
    // create the html list
    rootUl = new HtmlGenericControl("ul");
    
    // our name-control mapping
    var controls = new Dictionary<string, HtmlGenericControl>();              
    
    // our data
    var items = new[]{
        "fish>shark>mako",
        "fish>shark>tiger",
        "fish>clown fish>nemo",
        "birds>eagle>bald eagle",
        "birds>hawk",
    };        
    
    // loop through the data items
    foreach (var item in items)
    {
        // split by the greater-than symbol
        var names = item.Split('>');
    
        // if we don't already have a control for the topmost parent...
        if (!controls.TryGetValue(names[0], out parentLi))
        {
            // create the control and store it in our mapping
            controls[names[0]] = parentLi = new HtmlGenericControl("li")
            {
                InnerText = names[0]
            };               
            // add it to the html list
            rootUl.Controls.Add(parentLi);
        }
    
        // loop through the remaining parts
        foreach (var name in names.Skip(1))
        {
            // if the parent doesn't have a list-child...
            if (parentLi.Controls.Count == 1)
            {
                // add an html list child to the parent
                parentLi.Controls.Add(new HtmlGenericControl("ul"));
            }
    
            // if we don't already have a control for the child...
            if (!controls.TryGetValue(name, out childLi))
            {
                // create the control
                controls[name] = childLi = new HtmlGenericControl("li"){
                    InnerText = name
                };
            }
    
            // add the child to the parent's html list
            parentLi.Controls[1].Controls.Add(childLi);
    
            // keep a reference to the child for further nesting
            parentLi = childLi;
        }
    }
    
    // add the html list to the form
    form1.Controls.Add(rootUl);
    

    这导致以下输出:

    <ul>
      <li>fish
        <ul>
          <li>shark
            <ul>
              <li>mako</li>
            </ul>
          </li>
          <li>nemo</li>
        </ul>
      </li>
      <li>birds
        <ul>
          <li>eagle
            <ul>
              <li>bald eagle</li>
            </ul>
          </li>
          <li>hawk</li>
        </ul>
      </li>
    </ul>