使用List <string>动态写入文件:格式化问题

时间:2018-06-15 09:51:15

标签: c# string list

我编写了一个函数,可以让我动态编写包含一些代码的文件。我喂的方法点数。我已经钉了它,但我有一个愚蠢的格式问题。

以下代码(使用numberOfPoints = 3

private static void PointsGenerator(int numberOfPoints)
{
    string fileContent = Resources.Points_AutoGenerated;
    List<string> lines = new List<string>(fileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None));

    for (var j = 0; j < numberOfPoints - 1; j++)
    {
        int index = 0;

        for (var i = 1; !lines[i].Equals("ENDMODULE"); i++)
        {
            if (lines[i].Contains("! <<< POINT PLACEHOLDER >>>"))
            {
                if (numberOfPoints == 1)
                {
                    // Let's remove the placeholder...
                    lines.RemoveAt(i);

                    // ... and the extra new-line.
                    lines.RemoveAt(i);
                }
                else
                {
                    lines.RemoveAt(i);
                    lines.Insert(i, Resources.Snippet_POINT);
                    index = i;
                }
            }
        }

        if (j < numberOfPoints - 2)
        {
            lines.Insert(index + 2, "! <<< POINT PLACEHOLDER >>>");
        }
    }

    Console.WriteLine(string.Join(Environment.NewLine, lines.ToArray()));
}

正在输出此文件

! <<< Code generated with MG ABB Missions Generator. >>>

MODULE Points_AutoGenerated

    TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

    TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

    TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
ENDMODULE

我不喜欢它,因为我在ENDMODULE行之前错过了额外的新行。

但是,如果我使用第二个代码(只是粘贴我已经改变的部分)

if (j < numberOfPoints - 2)
{
    lines.Insert(index + 2, "! <<< POINT PLACEHOLDER >>>");
}
    else
{
    lines.Insert(index + 1, Environment.NewLine);
}

我得到了这个输出,而不是

! <<< Code generated with MG ABB Missions Generator. >>>

MODULE Points_AutoGenerated

    TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

    TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

    TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];


ENDMODULE

,与以前类似,我不喜欢因为最后的额外新行(我只想要其中一个!)。

我错过了什么?

这是我用来生成最终文件的两个模板:

Points_AutoGenerated.txt

! <<< Code generated with MG ABB Missions Generator. >>>

MODULE Points_AutoGenerated

    TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

    ! <<< POINT PLACEHOLDER >>>

ENDMODULE

Snippet_POINT.txt

    TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];

1 个答案:

答案 0 :(得分:3)

只需添加一个空字符串,就可以从WriteLine(Join())获取换行符。

else
{
    lines.Insert(index + 1, "");
}

// here the empty string becomes an empty line
Console.WriteLine(string.Join(Environment.NewLine, lines.ToArray()));