字符串到层次列表 [英] String to Hierarchical List

查看:63
本文介绍了字符串到层次列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串转换为层次结构列表,其中字符串中的每一行代表层次结构中的单个项目.

I'm trying to convert a string into a hierarchical list where each line in the string represents a single item within the hierarchy.

例如说我有以下字符串:

For example say I have the following string:

1 - First Level 1
2 - First Level 1 > Second Level 1
3 - First Level 1 > Second Level 1 > Third Level 1
4 - First Level 1 > Second Level 2
5 - First Level 2
6 - First Level 2 > Second Level 1
7 - First Level 2 > Second Level 1 > Third Level 1
...

我需要将其转换为以下类型的列表:

I need to convert it to a list of the following type:

public class Category {
    public int Id { get; set; }
    public string Name { get; set; }
    public Category Parent { get; set; }
}

类别名称不能包含-或>字符.

例如以下行:

3-第一层>第二层>第三层

3 - First Level 1 > Second Level 1 > Third Level 1

将在列表中添加一个ID为3,名称为第三级"的类别,并且父级将指向名称为第二级"的类别(在上面的示例中,id = 2,不是id = 6).

Would add a category to the list with an id of 3, a name of "Third Level 1" and the Parent would point to the category where the name is "Second Level 1" (id = 2 in the example above and not id = 6).

请注意,可能有多个具有相同名称的类别,因此需要查找整个路径以获取父项.

到目前为止,我已经设法按行分割字符串,然后针对每行对连字符进行另一个分割,以获取ID和完整的类别名称.然后,我可以对大于符号进行进一步拆分,以检索类别部分.我将使用最后一部分来获取类别名称,如果有多个部分,我知道我需要查找父项.

So far I have managed to split the string per line and then for each line I do a another split against the hyphen to get the id and full category name. I can then do a further split against the greater than symbol to retrieve the category parts. I take the last part to get the category name and if there is more than one part I know I need to lookup the parent.

这是我迷路的地方,因为我现在需要使用其余部分来计算父项,并考虑到以上考虑的多个类别可能具有相同的名称

This is where I get lost as I now need to use the remaining parts to work out the parent taking into my consideration above that multiple categories may have the same name

如果有人可以向我展示如何做到这一点,我将不胜感激.谢谢

I'd appreciate it if someone could show me how this can be done. Thanks

推荐答案

因为我更喜欢它,所以我使您的课程不可变:

Cause I like it more, i made your class immutable:

public class Category
{
    public int Id { get; private set; }
    public string Name { get; private set; }
    public Category Parent { get; private set; }

    public Category(int id, string name, Category parent)
    {
        Id = id;
        Name = name;
        Parent = parent;
    }

    public override string ToString()
    {
        return Id + " " + Name
            + (Parent == null ? String.Empty : (Environment.NewLine + "   Parent: " + Parent));
    }
}

通过使用此代码,我得到了所有可用类别的平坦列表,其中每个类别都引用了其父类别:

And by using this code I got a flat list of all available categories where each category gets a reference to its parent:

var categories = new Dictionary<String, Category>(StringComparer.InvariantCultureIgnoreCase);

using (var reader = new StringReader(_SampleData))
{
    string line;

    while ((line = reader.ReadLine()) != null)
    {
        if (String.IsNullOrWhiteSpace(line))
            continue;

        var elements = line.Split('-');
        var id = int.Parse(elements[0]);
        var name = elements[1].Trim();
        var index = name.LastIndexOf('>');
        Category parent = null;

        if (index >= 0)
        {
            var parentName = name.Substring(0, index).Trim();
            categories.TryGetValue(parentName, out parent);
        }

        var category = new Category(id, name, parent);
        categories.Add(category.Name, category);
    }
}

仅用于可视化调用:

foreach (var item in categories.Values)
{
    Console.WriteLine(item);
}

输出将是:

1 First Level 1
2 First Level 1 > Second Level 1
   Parent: 1 First Level 1
3 First Level 1 > Second Level 1 > Third Level 1
   Parent: 2 First Level 1 > Second Level 1
   Parent: 1 First Level 1
4 First Level 1 > Second Level 2
   Parent: 1 First Level 1
5 First Level 2
6 First Level 2 > Second Level 1
   Parent: 5 First Level 2
7 First Level 2 > Second Level 1 > Third Level 1
   Parent: 6 First Level 2 > Second Level 1
   Parent: 5 First Level 2

这篇关于字符串到层次列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆