将带有子字符串的字符串按正确的顺序转换为对象列表

Convert string with substrings into list of objects in correct order

提问人:IAMABANANA 提问时间:5/12/2023 最后编辑:IAMABANANA 更新时间:5/12/2023 访问量:39

问:

好的,我有一种复杂的字符串列表,我需要将其转换为新的输出。我觉得我可能走错了路,或者它就是这么复杂?它会做大部分的事情,但是一旦它完成第三个循环并返回第二个循环,它就会添加一个空对象,然后再次添加第三个对象并出错。有没有其他方法可以做到这一点或修复我所拥有的?

任何帮助或想法都会很棒:(

注意:字符串中的名称为隐私而更改,它们通常不是 string1 等。

(String1, string2, string3, string4(string5, string6, string7(c1, c2, c3)), string8)

TO

- string1
- string2
- string3
- string4
  - string5
  - string6
  - string7
    - c1
    - c2
    - c3
-string8

我的代码

 public List<object> ParseOutput(string parseString) {
    List<object> sortedList = new List<object>();

    int indexFirstLoop = 0;
    int indexSecondLoop = 0;
    int indexThirdLoop = 0;
    int stringLength = parseString.Length - 1;

    //Iternate through string to find comma seperated words
    for (int a = 0; a < parseString.Length; a++)
        {
            //Hits a comma, take word before comma and add to array
            if(parseString[a] == ','){
                //get word before comma and put into array
                sortedList.Add(parseString.Substring(indexFirstLoop, a - indexFirstLoop).Trim());
                indexFirstLoop = a + 1;
            }
            //If hits open parenthesis then go to next section and loop through those strings
            else if(parseString[a] == '('){
                var secondString = parseString.Substring(a + 1, stringLength - a);
                var secondList = new List<string>();
                //Second Loop
                for (int b = 0; b < secondString.Length; b++)
                {
                    //Hits a comma, take word before comma and add to array
                    if(secondString[b] != ')'){
                        if(secondString[b] == ','){
                            secondList.Add(secondString.Substring(indexSecondLoop, b - indexSecondLoop).Trim());
                            indexSecondLoop = b + 1;
                        }
                    
                    //If hits open parenthesis inside first parentethsis, then go to next section and loop through those strings
                    else if(parseString[b] == '('){
                        var secondStringLength = secondString.Length;
                        var thirdString = secondString.Substring(b + 2, secondStringLength - (b + 2));
                        var thirdList = new List<string>();
                        //Third Loop
                        for (int c = 0; c < thirdString.Length; c++)
                        {
                            //Hits a comma, take word before comma and add to array
                            if(thirdString[c] == ','){
                                thirdList.Add(thirdString.Substring(indexThirdLoop, c - indexThirdLoop).Trim());
                                indexThirdLoop = c + 1;
                            }
                        } //End Last Loop
                        if(thirdList.Count > 0){
                            sortedList.Add(thirdList);
                        }
                    } //End Second Parenthesis Loop
                    }
                }//End Second Loop

                if(secondList.Count > 0){
                    sortedList.Add(secondList);
                }

            } //End First Parenthesis Loop
        } //End First Loop

    return sortedList;
}
C# 字符串 列表 解析数据 操作

评论

0赞 DonMiguelSanchez 5/16/2023
这里的目标是什么?您希望获得哪个对象或输出?

答: 暂无答案