Split a separated string into hierarchy using c# and linq

后端 未结 3 1181
独厮守ぢ
独厮守ぢ 2021-02-14 23:02

I have string separated by dot (\'.\') characters that represents a hierarchy:

string source = \"Class1.StructA.StructB.StructC.FieldA\";

How c

3条回答
  •  臣服心动
    2021-02-14 23:33

    Here's a solution completely without LINQ:

    public static string[] GetHierarchy(this string path)
    {
        var res = path.Split('.');
        string last = null;
        for (int i = 0; i < res.Length; ++i)
        {
            last = string.Format("{0}{1}{2}", last, last != null ? "." : null, res[i]);
            res[i] = last;
        }
        return res;
    }
    

提交回复
热议问题