Split a separated string into hierarchy using c# and linq

后端 未结 4 1446
粉色の甜心
粉色の甜心 2021-02-14 23:28

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

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

How c

4条回答
  •  广开言路
    2021-02-14 23:46

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

提交回复
热议问题